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

时间:2015-02-25 00:10:17

标签: php

<?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");
}
?>

2 个答案:

答案 0 :(得分:11)

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

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

答案 1 :(得分:0)

请注意,如果您使用的是CODEIGNITER,请不要在文件路径中使用base_url(),而应使用常规路径

相关问题