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

phpis_file(string $filename): bool

Example

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

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

Code execution result (if the file exists):

phptrue

Example

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

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

Code execution result:

phpfalse

Example

Let's check several paths at once:

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

Code execution result:

phpfile1.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