我正在处理一个有文件上传的项目。这些文件被赋予一个随机名称并放置在 Web 目录的一侧。
然后,我有一个脚本,该脚本根据URL中传递的参数检索此图像,该脚本绝对可以正常工作。
但是,我制作了一个要传递给浏览器的标头数组,然后用foreach
循环遍历这些标头,它们似乎没有设置任何内容。但是,如果我使用 header
功能手动设置它们,它们就可以工作!奇怪!
现在,我不介意使用 header
函数而不是遍历它们,但是,是否有特定原因导致这不起作用?
我还使用上述方法将所有标头存储在数组中,然后在将内容交付到浏览器时处理它们,但是,我不能确定它们是否真的被传递,看到上述问题!
我的代码片段:
// Expiry date in seconds, in this instance a year
$expiry_date= ( 60 * 60 * 24 * 365 );
// Our headers
$img_headers= array(
'Content-Type : ' . $mime,
'Cache-Control: no-cache, must-revalidate',
//'Cache-control: max-age=' . $expiry_date,
'Expires:' . gmdate( DATE_RFC1123, time() + $expiry_date ),
'Pragma: ',
'Last-Modified: ' . gmdate( gmdate( DATE_RFC1123 ), filemtime( $image_path ) ),
'Content-Length: ' . ( string ) filesize( $image_path ),
'Content-Disposition: inline; filename="' . $image_name . '"'
);
/*header( 'Content-Type: ' . $mime, TRUE );
header( 'Cache-Control: no-cache, must-revalidate', TRUE );
header( 'Expires: ' . gmdate( DATE_RFC1123, time() + $expiry_date ) );
header( 'Pragma: ', TRUE );
header( 'Last-Modified: ' . gmdate( gmdate( DATE_RFC1123 ), filemtime( $image_path ) ), TRUE );
header( 'Content-Length: ' . ( string ) filesize( $image_path ), TRUE );*/
// Loop through and set up our headers!
foreach( $img_headers as $new_header )
{
header( $new_header, TRUE );
}
// Read our image and end the rest of the file execution
return die( readfile( $image_path ) );
那里有什么看起来不合适的吗?
提前感谢!
这两种
方法应该是相同的。 也许"内容类型:"中的额外空间导致了问题?
尝试用以下内容替换您的foreach循环,我曾经遇到过类似的问题,空格会产生逻辑错误,值得一试。
foreach( $img_headers as $new_header )
{
header( trim($new_header), TRUE );
}