filetype
-
PHP’s
filetypeperforms astaton a file and returns a string representing its type.PHP
$type = filetype('/path/to/foobar'); var_export($type); //=> "file"
The
File.ftypeclass method in Ruby performs the same task.Ruby
type = File.ftype('/path/to/foobar') p type #=> "file"
This table shows the strings returned by each version.
PHP Ruby link link fifo fifo char characterSpecial dir directory block blockSpecial file file socket No equivalent unknown unknown Checking for a Specific File Type
Perhaps the most common usage of
filetypeis to check if a path is a specific type, such as this example which checks if the path is adir.PHP
if (filetype('/path/to/foobar') == 'dir') { // ... }
You can do this with the string returned by
File.ftypeas well butFilealso provides convenience methods likefile?anddirectory?that make code more readable.Ruby
if File.directory?('/path/to/foobar') # ... end
Working with File::Stat Instances
The
File::Statinstance returned by Ruby’sFile.statalso supports the methods shown above and more.Ruby
stat = File.stat('/path/to/foobar') p stat.ftype #=> "file"
Ruby
stat = File.stat('/path/to/foobar') if stat.directory?('/path/to/foobar') # ... end
see also
Looking for Rails or PHP web application development, integration, and training?
Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.

