Hudzilla.org - the homepage of Paul Hudson
Contents > Files Wish List | Report Bug | About Me ]

8.6     Checking whether a file exists: file_exists()

This is NOT the latest copy of this book; click here for the latest version.

bool file_exists ( string filename)

The act of checking whether a file exists is one of the most basic file-related tasks you'll want to do, and thankfully file_exists() makes it as easy as it should be. In fact, it's so easy you should be able to guess the parameter list and return value!

Still here? Oh, alright then - you specify the filename to check as the only parameter, and it returns true if the file exists and false otherwise. Example:

<?php
    
if (file_exists("snapshot1.png")) {
        print
"Snapshot1.png exists!\n";
    } else {
        print
"Snapshot1.png does not exist!\n";
    }
?>

Although you're only part-way through this book, I hope you managed to guess how it worked yourself!

Author's Note: The result of file_exists() is cached, which means you first need to call the clearstatcache() function if you want to be absolutely sure a file exists.





<< 8.5 Other file functions: rewind(), and fseek()   8.7 Retrieving a file's status: is_readable(), is_writeable(), is_executable(), is_file(), is_dir(), and clearstatcache() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 06 Sep 2008

<?php

$d = "\\scripts\\titlebar.js";

print $d . "<br>";
clearstatcache();
if (file_exists($d)){
print "y";
}else {
print "n";
}

?>

the expected result is "y" being printed. this is never the case however. I right clicked my navigation tool's shortcut to the file so i know for a fact that the link is valid. The only way it seems to work is if i call a file in the folder of the php file. this makes navigation and directory management impossible. any ideas?

A PHP User - 06 Sep 2008

<?php

$d = "\\scripts\\titlebar.js";

print $d . "<br>";
clearstatcache();
if (file_exists($d)){
print "y";
}else {
print "n";
}

?>

the expected result is "y" being printed. this is never the case however. I right clicked my navigation tool's shortcut to the file so i know for a fact that the link is valid. The only way it seems to work is if i call a file in the folder of the php file. this makes navigation and directory management impossible. any ideas?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is three plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow