PHP › File Handling & File System Functions
How do you check whether a file or directory exists, and how do you retrieve useful metadata like a file's size or last modified time?
IntermediateAnswer
The file_exists function checks whether a given path exists as either a file or a directory, while functions like filesize and filemtime retrieve specific metadata about a file, such as its size in bytes and the timestamp it was last modified, all of which are commonly used to validate assumptions before performing an operation, such as confirming a required configuration file actually exists before attempting to read it.
if (file_exists('config.php')) {
echo 'Last modified: ' . date('Y-m-d', filemtime('config.php'));
}Real-world example An application checks whether a specific uploaded file already exists before allowing a user to upload another file with the same name, preventing an accidental overwrite of previously uploaded content.