PHP is_file() Function
Quick summary: The PHP is_file() function checks whether a path is a regular file.
PHP is_file() Syntax
is_file(string $filename): bool
PHP
PHP is_file() Basic examples
var_dump(is_file('config.php'));
PHP
Output:
bool(true)
Returns true for regular files.
PHP is_file() Real-world usage
if (is_file('upload.txt')) {
echo 'Valid file';
}
PHP
Output:
Valid file
Validates uploaded file paths.
PHP is_file() Edge cases
var_dump(is_file('logs'));
PHP
Output:
bool(false)
Directories are not files.
PHP is_file() Common mistakes
Using is_file instead of file_exists
is_file returns false for directories.
Incorrect
is_file('some/path');
Correct
file_exists('some/path');
Choose the correct check based on intent.
PHP is_file() Frequently Asked Questions
What does is_file() do in PHP?
Checks whether a path is a regular file.
Difference between is_file() and file_exists()?
is_file() returns true only for files, not directories.
Does is_file() work with directories?
No, it returns false for directories.
Use case of is_file()?
Validating file paths before reading.
Can it return false even if file exists?
Yes, due to permissions or caching.
Performance of is_file()?
Fast filesystem check.
Common mistake?
Confusing it with file_exists().
Works with symlinks?
Yes, if target is a file.
Safe for user input?
Validate paths first.
Alternative?
file_exists() or is_readable().
Can be cached?
Yes, clearstatcache() may be needed.
Best practice?
Combine with is_readable().