Check if a file or directory exists in PHP

Description

The following code shows how to check if a file or directory exists.

Example


/*from ww  w  .j  av  a  2 s .  c o  m*/
<?php
echo file_exists("test.txt");
    
$filename = '/path/to/foo.txt';

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}    
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    File »




Directory
DirectoryIterator
Drive
File
File Permission
File Read Save
FileSystemIterator
Path
Zip