Checking File Existence in PHP
The function file_exists
checks whether
a file, whose path is passed as a parameter, exists.
Example:
var_dump(file_exists('test.txt')); // true or false
Typically, this function is used to check for a file's presence before performing an operation on it. For example, like this:
<?php
if (file_exists('test.txt')) {
echo filesize('test.txt');
} else {
echo 'file does not exist';
}
?>
Check if the file file.txt
is located in the root of your site.
Check if the file file.txt
is located in the root of your site. If not - create
it and write the text '!'
into it.
Check if the file message.txt
is located in the root of your site. If such a file
exists - output the text of this file to the screen.