266 of 410 menu

The is_file Function

The is_file function checks whether a file exists at the specified path and whether it is a regular file (not a directory or symbolic link). The function returns true if the file exists and is a regular file, and false otherwise.

Syntax

is_file(string $filename): bool

Example

Let's check the existence of the file 'test.txt':

<?php $res = is_file('test.txt'); var_dump($res); ?>

Code execution result (if the file exists):

true

Example

Let's check the existence of a directory (the function will return false):

<?php $res = is_file('directory_name'); var_dump($res); ?>

Code execution result:

false

Example

Let's check several paths at once:

<?php $files = ['file1.txt', 'file2.txt', 'folder']; foreach ($files as $file) { echo "$file: " . (is_file($file) ? 'true' : 'false') . "\n"; } ?>

Code execution result:

file1.txt: true file2.txt: false folder: false

See Also

  • the file_exists function,
    which checks for file existence
  • the is_dir function,
    which checks for a directory
  • the is_writable function,
    which checks write permissions
byenru