Filemtime中的PHP警告()&无法修改标头信息 - 标头已发送错误

时间:2011-04-04 04:39:39

标签: php debugging header

在@James的帮助下,图像生成器处于良好状态。现在,当文件或文件夹不存在时,图像引擎会输出一个微小但奇怪的错误。确实应该有 错误,但我不确定下面会出现这个错误。以下是谷歌尝试通过imgcpu.php缩略图生成器获取旧的不存在的文件夹/ image.jpg时发生的情况:

[20:24:25] PHP Warning: filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for old_folder/old-image.jpg in imgcpu.php on line 802
[20:24:25] PHP Warning: Cannot modify header information - headers already sent by (output started at imgcpu.php:802) in imgcpu.php on line 201

[20:26:31] PHP Warning: filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for old_folder/old-image.jpg in imgcpu.php on line 802
[20:26:31] PHP Warning: Cannot modify header information - headers already sent by (output started at imgcpu.php:802) in imgcpu.php on line 201

[20:28:24] PHP Warning: filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for old_folder/old-image.jpg in imgcpu.php on line 802
[20:28:24] PHP Warning: Cannot modify header information - headers already sent by (output started at imgcpu.php:802) in imgcpu.php on line 201

[20:31:03] PHP Warning: filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for old_folder/old-image.jpg in imgcpu.php on line 802
[20:31:03] PHP Warning: Cannot modify header information - headers already sent by (output started at imgcpu.php:802) in imgcpu.php on line 201 

第201行和第802行似乎与它有关:

line 201**     header('HTTP/1.1 400 Bad Request');
line 202       die($message);
....
line 801       header("Content-type: " . $this->_mime);
line 802**     $last_modified = filemtime($source);

问题:错误是否正确?或警告是否应以某种方式解决?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:2)

我想,如果该文件不存在,您不应该尝试获取文件的最后修改日期。

首先,您应该使用file_exists()函数检查文件是否存在。


我想你应该使用这样的东西:

if (file_exists($source)) {
    $last_modified = filemtime($source);
    // Use the $last_modified variable
}
else {
    // the file doesn't exist => 404
    header('HTTP/1.1 404 Not Found');
    die;
}