PHP Filemtime()中的警告&不能修改标题信息 - 标题已发送错误
问题描述:
在@James的帮助下,图像生成器处于良好状态。现在,当文件或文件夹不存在时,图像引擎会输出一个微小但奇怪的错误。确实应该有和错误,但我不确定它应该是下面的这一个。下面是当谷歌试图通过imgcpu.php缩略图生成器来获取一个老不存在的文件夹/ image.jpg的会发生什么:PHP Filemtime()中的警告&不能修改标题信息 - 标题已发送错误
[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);
问题:错误是否正确?或者应该以某种方式解决警告?如果是这样,怎么样?
答
我想,如果该文件不存在,您不应该尝试获取文件的上次修改日期。
您应该检查文件是否存在,首先使用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;
}
从filemtime错误消息是打破头输出,所以修复filemtime错误。 – 2011-04-04 04:44:37