Are there any potential pitfalls to be aware of when using PHP to check for the existence of a file?
When using PHP to check for the existence of a file, one potential pitfall to be aware of is that the file path provided may be vulnerable to directory traversal attacks if not properly sanitized. To mitigate this risk, it is important to validate and sanitize the file path input before using it in the file existence check.
// Validate and sanitize the file path input
$filePath = filter_var($_GET['file'], FILTER_SANITIZE_STRING);
// Check if the file exists
if (file_exists($filePath)) {
echo 'File exists';
} else {
echo 'File does not exist';
}