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