#52: Checking to See If a File Exists

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:

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

Get Wicked Cool PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.