filetype() Function is one of the PHP operations used in filesystem and stream workflows. Returns the type of the path as a short string, such as file or dir. The examples below show what it does and how to handle its result.
What Is filetype() Function?
Returns the type of the path as a short string, such as file or dir.
Its syntax is:
filetype(string $filename)
The parameters are:
string $filename— The source or destination filesystem path.
Return value: The declared return type is string|false. Symbolic links may report link; use is_file() when you specifically need regular files.
Important: Symbolic links may report link; use is_file() when you specifically need regular files.
Example 1: Basic Usage
The basic example focuses on the behavior of filetype() Function: returns the type of the path as a short string, such as file or dir. Inputs are defined locally or shown as application-controlled paths.
<?php
echo filetype(__FILE__);
echo "\n" . filetype(__DIR__);
The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.
Example 2: A Different Use Case
This second example illustrates another input or execution condition for filetype() Function. Notice the different checks, arguments, or output handling.
<?php
$type = @filetype(__DIR__ . "/missing-file");
var_dump($type === false);
This variation matters because symbolic links may report link; use is_file() when you specifically need regular files.
Practical Example: Using the Operation in an Application
The following example demonstrates how filetype() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.
<?php
$path = __DIR__ . "/config.json";
$type = @filetype($path);
if ($type === "file") { echo "Regular file"; } elseif ($type === false) { echo "Missing path"; }
The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.
Important Behavior and Edge Cases
- Function behavior: Returns the type of the path as a short string, such as file or dir.
- Limitation: Symbolic links may report link; use is_file() when you specifically need regular files.
Comparison With Related Functions
| Function or Method | Difference |
|---|---|
filetype() Function | Returns the type of the path as a short string, such as file or dir. |
is_file() | Answers whether the target is a regular file; filetype() returns a string representing its type. |
Use filetype() Function for the specific behavior described above. The related operation, is_file(), serves a different purpose; do not substitute it unless that different behavior is intended.
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming filesystem metadata is always available and current. | Symbolic links may report link; use is_file() when you specifically need regular files. Check failure returns and refresh stat information when files may have changed. |
| Treating is_file() as an interchangeable replacement for filetype() Function. | Answers whether the target is a regular file; filetype() returns a string representing its type. |
Key Takeaways
- Returns the type of the path as a short string, such as file or dir.
- Symbolic links may report link; use is_file() when you specifically need regular files.
- Related operation: is_file() — Answers whether the target is a regular file; filetype() returns a string representing its type.
filetype() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.