PHP Checking to See If a File Exists

Aug 31, 2009 Author: vvaswani

fopen() generates error messages if you're trying to open a file that doesn't exist, as do other functions such as unlink(), when you attempt to delete a nonexistent file. To see if a file exists before you perform a file operation, use the file_exists() function in a test condition:

<?php
if (file_exists('file.txt')) {
    print 'OK, file.txt exists.';
}
?>

As you can see, this function returns true if the file exists and false if it does not.


views 4054
  1. Add New Comment