filetype

Returns the type of a path as a string, such as file, dir, link or fifo.

Signature
filetype(string $filename): string|false
  • Added in PHP 4.0.0

Parameters

ParameterTypeDescription
$filenamestringPath to the file.

Return value

string|falseOne of fifo, char, dir, block, link, file, socket or unknown, or false on failure.

Errors

Emits an E_WARNING if the stat call fails, and an E_NOTICE if the type cannot be determined.

filetype turns the type bits of a stat() result into a readable string. The possible values are fifo, char, dir, block, link, file, socket and unknown.

When to reach for it

  • Walking a directory tree and branching on what each entry actually is.
  • Logging or displaying entry types in a file browser.
  • Rejecting anything that is not a plain file before processing it.

What trips people up

The big one: filetype does not follow symlinks. It is lstat-based, so a symlink reports link — never the type of whatever it points at, and whether or not the target still exists. If you want the target's type, run is_file() or is_dir() on the path, or resolve it with realpath() first; is_link() remains the clean boolean test for the link itself.

For a simple yes/no question, is_file() and is_dir() read better and return a clean boolean. Reach for filetype when you genuinely need to distinguish more than two cases, or want the type name for display.

Beware of comparing the result loosely. filetype() can return false, and false == 'file' is false in PHP 8 — but a strict === comparison is still the habit to build. Results are stat-cached like the rest of the family.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Skip directories, sockets and pipes while iterating a folder you only want files from.
  • Show an icon per entry type in an admin file listing.
  • Detect that a path you expected to be a FIFO is actually a regular file.

Frequently asked questions

Why does filetype return "link" for my symlink instead of the target's type?

Because `filetype` describes the path itself rather than what it points at — it is lstat-based and does not follow symlinks, which is why `link` is one of its documented return values. Resolve the link with `realpath()` first, or use `is_file()`/`is_dir()`, which do follow symlinks.

What is the difference between filetype and is_dir?

`is_dir` returns a boolean for one specific question, while `filetype` returns a string naming any of eight possible types. Use `is_dir` for a yes/no check and `filetype` when you need to distinguish more than two cases.

What values can filetype return?

`fifo`, `char`, `dir`, `block`, `link`, `file`, `socket`, `unknown`, or `false` if the stat call failed. The last two are distinct: `unknown` means the path was stat'ed but the type was not recognised.

More examples

Filtering a directory listing by type

Each entry prints alongside `file` or `dir`, which is enough to branch on while walking a tree. Note the explicit skip of `.` and `..` — `scandir` always returns them and they would both report `dir`.

Example · php
<?php
mkdir('scan-demo');
file_put_contents('scan-demo/a.txt', 'a');
file_put_contents('scan-demo/b.txt', 'b');
mkdir('scan-demo/sub');
clearstatcache();

foreach (scandir('scan-demo') as $entry) {
    if ($entry === '.' || $entry === '..') continue;
    echo str_pad($entry, 10), filetype("scan-demo/$entry"), "\n";
}

unlink('scan-demo/a.txt');
unlink('scan-demo/b.txt');
rmdir('scan-demo/sub');
rmdir('scan-demo');

filetype versus is_file and is_dir

All four dumps print `bool(true)` — the pairs are equivalent for the common cases. `is_file()`/`is_dir()` are clearer for a two-way test; `filetype()` earns its place when you need to name the type or handle sockets and pipes too.

Example · php
<?php
file_put_contents('probe.txt', 'x');
clearstatcache();

var_dump(filetype('probe.txt') === 'file');
var_dump(is_file('probe.txt'));
var_dump(filetype('.') === 'dir');
var_dump(is_dir('.'));

unlink('probe.txt');

Handling a path that does not exist

The first path prints `dir`; the second prints the failure branch because `filetype` returned `false` after its warning was suppressed. Always compare with `=== false` before using the string.

Example · php
<?php
$paths = ['.', 'definitely-not-here'];

foreach ($paths as $p) {
    $type = @filetype($p);
    if ($type === false) {
        echo "$p: unable to determine type\n";
        continue;
    }
    echo "$p: $type\n";
}

Discussion

  • Be the first to comment on this lesson.