循环,一组标题......没有任何事情发生

时间:2012-04-15 01:30:54

标签: php header foreach

我正在开发一个包含文件上传的项目。这些文件具有随机名称,并放在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 ) );

那里看起来不合适的东西?

提前致谢!

2 个答案:

答案 0 :(得分:1)

这两种方法应该是相同的。也许“内容类型:”中的额外空间导致问题?

答案 1 :(得分:0)

尝试用以下代码替换你的foreach循环,我有一个类似的问题,一旦空白区域产生逻辑错误,它值得一试。

foreach( $img_headers as $new_header )
{
    header( trim($new_header), TRUE );
}