我有一个从数据库返回图像的函数,该图像在数据库中是 100% 正常的,但是当它被发送到浏览器时,一个换行符(返回)被插入到最开始,它会损坏文件。
返回文件的代码是:
//if(ob_get_length() > 0)
//{
//ob_clean();
//}
将上面的内容注释掉,因为当它被取消注释时,它甚至不会让浏览器下载文件。
function header_file($data, $file_data)
{
$last_modified = gmdate('D, d M Y H:i:s', $file_data['unix_last_modified_time'])." GMT";
// update the accessed record
$GLOBALS['db']->query("update file_uploads set file_upload_accessed_count = file_upload_accessed_count + 1 where file_upload_id = '".$GLOBALS['id']."'");
// if browser question if it's up to date
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
{
// parse header
$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
if ($if_modified_since == $last_modified)
{
// the browser's cache is still up to date
header("HTTP/1.0 304 Not Modified");
header("Cache-Control: max-age=86400, must-revalidate");
exit;
}
}
header("Cache-Control: max-age=86400, must-revalidate");
header("Last-Modified: ".$last_modified);
header("Content-Type: ".$file_data['file_upload_type']);
if($file_data['file_upload_type'] == 'application/x-shockwave-flash')
header("Content-Disposition: inline; filename=\"".str_replace(' ','_',$file_data['file_upload_name'])."\"");
else
header("Content-Disposition: attachment; filename=\"".str_replace(' ','_',$file_data['file_upload_name'])."\"");
// send data to output
echo $data;
exit;
}
相关问题: 从数据库返回的 PHP 头文件损坏的文件? https://stackoverflow.com/questions/19768650/zend-caching-of-images-gives-problems-once-the-site-goes-down-for-a-while