W3docs

filetype()

The filetype() function is a built-in PHP function that returns the type of a file. This function returns the file type as a string.

What is the filetype() Function?

filetype() is a built-in PHP function that tells you what kind of filesystem entry a path points to — a regular file, a directory, a symbolic link, and so on. It takes a single path and returns the type as a lowercase string, or false on failure.

This is useful when you walk a directory and need to treat files and subdirectories differently, when you validate that an upload landed where you expected, or when you need to skip special entries (pipes, device files) that should not be read like ordinary files.

Here is the syntax:

The PHP syntax of filetype()

filetype(string $filename): string|false

Where $filename is a relative or absolute path to the entry you want to inspect.

Return Values

filetype() returns one of these strings:

Returned stringMeaning
fileA regular file
dirA directory
linkA symbolic link
fifoA named pipe (FIFO)
socketA Unix domain socket
blockA block-special device (e.g. a disk)
charA character-special device (e.g. a terminal)
unknownThe type could not be determined

If the path does not exist or cannot be read, filetype() returns false and emits an E_WARNING. Because false compares as equal to the empty string in a loose comparison, always check the result with === before trusting it.

How to Use the filetype() Function?

Pass a path and inspect the returned string:

Basic usage

<?php

$path = __FILE__;          // the path of the currently running script
$type = filetype($path);

echo "The entry '$path' is of type: $type";
// e.g. The entry '/var/www/index.php' is of type: file

Using __FILE__ guarantees the path exists, so the example runs anywhere without setup.

Handling the false Return

Since filetype() returns false for a missing or unreadable path, guard the call before you use the result. Checking with file_exists() first avoids the warning entirely:

Safe lookup

<?php

$path = '/path/that/does/not/exist';

if (!file_exists($path)) {
    echo "Path not found.";
} else {
    $type = filetype($path);
    echo $type === false
        ? "Could not determine the type."
        : "Type: $type";
}
// Path not found.

Telling Files from Directories While Looping

A common real task is iterating a directory and acting only on files or only on subdirectories. filetype() makes the branch explicit:

Classify directory entries

<?php

$dir = sys_get_temp_dir();          // a directory that always exists

foreach (scandir($dir) as $entry) {
    if ($entry === '.' || $entry === '..') {
        continue;                   // skip the self/parent references
    }

    $full = $dir . DIRECTORY_SEPARATOR . $entry;
    echo $entry . ' => ' . filetype($full) . PHP_EOL;
}

Here scandir() lists the directory contents, and filetype() labels each entry. For lower-level traversal you can use readdir() with opendir() instead.

filetype() vs. is_file() and is_dir()

When you only need a yes/no answer for one specific type, the dedicated predicate functions are clearer and don't require a string comparison:

  • is_file()true if the path is a regular file.
  • is_dir()true if the path is a directory.

Reach for filetype() when you need to distinguish among several possible types in one place (for example to skip FIFOs and sockets), and use is_file() / is_dir() when a single boolean check reads better.

Watch Out for the Stat Cache

PHP caches the results of filesystem functions for the duration of a request, so if a path is created, deleted, or replaced after the first check, filetype() may report a stale value. Call clearstatcache() to force a fresh lookup. Note also that filetype() follows symbolic links the way stat() does — to detect the link itself, use lstat().

Conclusion

filetype() returns the kind of a filesystem entry as a string (file, dir, link, and so on) and false on failure. Guard the result with ===, check the path with file_exists() to avoid warnings, and remember clearstatcache() when re-checking a path that changed mid-request. For simple single-type checks prefer is_file() and is_dir(); for the full set of file statistics in one call, see stat().

Practice

Practice
What does the is_file() function in PHP do?
What does the is_file() function in PHP do?
Was this page helpful?