PHP filetype() Function
The PHP filetype() function returns the type of the specified file.
Note: The results of this function are cached. Use clearstatcache() function to clear the cache.
Syntax
filetype(filename)
Parameters
filename |
Required. Specify the path to the file to check. |
Return Value
Returns the type of the file. Possible values are:
- fifo
- char
- dir
- block
- link
- file
- socket
- unknown
Returns false if an error occurs. It will also produce an E_NOTICE message if the stat call fails or if the file type is unknown.
Exceptions
Upon failure, an E_WARNING is thrown.
Example:
Lets assume that we have a file called test.txt in the current working directory. The example below demonstrates on using this function to get the file type.
<?php //getting the file type echo "file type: ".filetype("test.txt")."\n"; //current directory echo "file type: ".filetype("/")."\n"; //one directory up echo "file type: ".filetype("..")."\n"; ?>
The output of the above code will be:
file type: file file type: dir file type: dir
❮ PHP Filesystem Reference