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


PHP: if file doesn't exist then exit script

<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
    echo "The file $filename exists";
}
?>

该脚本仅检查文件是否存在,但是我该如何执行此类操作?

<?php
$filename = '/path/to/foo.txt';
if (file doesnt exist) {
    exit("Your file doesn't exist");
}
?>

正如 John Conde 所说,你应该否定file_exists函数:

if (!file_exists($filename)) {
    exit("Your file doesn't exist");
}
如果您

使用的是 CODEIGNITER,请注意,不要在文件路径中使用base_url(),请使用普通路径