PHP:如果文件不存在则退出脚本

Lin*_*Lin 4 php

<?php
$filename = '/path/to/foo.txt';

if (file_exists($filename)) {
    echo "The file $filename exists";
}
?>
Run Code Online (Sandbox Code Playgroud)

该脚本仅检查文件是否存在,但我该如何做这样的事情?

<?php
$filename = '/path/to/foo.txt';

if (file doesnt exist) {
    exit("Your file doesn't exist");
}
?>
Run Code Online (Sandbox Code Playgroud)

Pat*_*ard 13

正如John Conde所说,你应该否定这个file_exists功能:

if (!file_exists($filename)) {
    exit("Your file doesn't exist");
}
Run Code Online (Sandbox Code Playgroud)