In PHP, filetype() tells you what kind of item a path points to, such as a file, a directory, or a link.
It returns one of the words fifo, char, dir, block, link, file, socket or unknown. For everyday work you will mostly see file and dir.
Syntax
Call filetype() with the values you need. Start with this line.
PHP
$type = filetype('notes.txt');
Call
What it does
filetype('notes.txt')
Returns "file" for a normal file.
filetype(__DIR__)
Returns "dir" for a directory.
filetype('shortcut')
Returns "link" when the path is a symbolic link.
filetype() looks at the link itself and not at its target, so a symbolic link gives link. If you only need a yes or no answer, is_file() and is_dir() are simpler.
Library declarations
PHP
filetype(string $filename): string|false
Parameters
Parameter
Description
filename
Required. The path to the item you want to check.
Return Value
Return Value
Description
string
Returns one of fifo, char, dir, block, link, file, socket or unknown.
false
Returns false on failure, for example when the path does not exist.
PHP Examples
Example 1 — Check a File and a Folder
Print the type of the current script and of the folder it lives in.