PHP filetype() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

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');
CallWhat 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

ParameterDescription
filenameRequired. The path to the item you want to check.

Return Value

Return ValueDescription
stringReturns one of fifo, char, dir, block, link, file, socket or unknown.
falseReturns 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.

PHP
<?php
echo basename(__FILE__), ': ', filetype(__FILE__), PHP_EOL;
echo 'Folder: ', filetype(__DIR__), PHP_EOL;

How It Works

  1. __FILE__ is the full path of the running script and __DIR__ is its folder.
  2. filetype() returns file for the script.
  3. filetype() returns dir for the folder.
  4. basename() keeps the output short by showing only the file name.

Example 2 — A Path That Does Not Exist

filetype() returns false for a missing path, so check it before you print.

PHP
<?php
$path = __DIR__ . '/nothing-here';

$type = @filetype($path);

if ($type === false) {
    echo "Path not found", PHP_EOL;
} else {
    echo "Type: $type", PHP_EOL;
}

How It Works

  1. The path nothing-here does not exist.
  2. filetype() fails and returns false. The @ sign hides the warning.
  3. The strict check catches the failure.
  4. The script prints a clear message.

Example 3 — Label a Mix of Items

Create one file and one folder and print the type of each.

PHP
<?php
$file   = __DIR__ . '/notes.txt';
$folder = __DIR__ . '/photos';

file_put_contents($file, 'hello');
mkdir($folder);

foreach ([$file, $folder] as $path) {
    echo basename($path), ': ', filetype($path), PHP_EOL;
}

unlink($file);
rmdir($folder);

How It Works

  1. file_put_contents() makes a file and mkdir() makes an empty folder.
  2. foreach goes through both paths one by one.
  3. filetype() returns file for the first and dir for the second.
  4. unlink() and rmdir() clean up the demo items at the end.

Frequently Asked Questions

It returns the type of a path as a word, such as file, dir or link.
fifo, char, dir, block, link, file, socket or unknown. If it fails, it returns false.
filetype() gives the name of the type. is_file() and is_dir() only answer true or false, and is_file() also follows symbolic links.
No. It reports link for a symbolic link. Use is_link() to test for links, or realpath() to find where the link points.
It returns false and raises a warning. Check with file_exists() or use === false.

Next: flock()

flock() locks a file so that only one script can write to it at a time. This is called adv.

flock() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful