Home / PHP Tips Generated By ChatGPT / Checking If a File or Directory Exists in PHP Use file_exists() to check if a file or directory exists. Source Code $filename = "test.txt"; if (file_exists($filename)) { echo "The file $filename exists."; } else { echo "The file $filename does not exist."; }