日期:2014-05-17 浏览次数:20560 次
<?php
$file = 'test_img.bmp';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
//给你一段
//$mime,文件类型
//$filename,这个不用说了吧
//$data 是你要输出的文件数据,你试试用 file_get_contents 来获得
if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
{
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".strlen($data));
}
else
{
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".strlen($data));
}
exit($data);
------解决方案--------------------
刚用了你的代码测试了一下,没出现你说的问题。
ob_clean();
flush();
把这两句去掉看看。
------解决方案--------------------
在你的这行代码前,看是否有输出过字符??
------解决方案--------------------
多出个换行符,,,贴的不是你生产环境的代码吧
------解决方案--------------------
源代码如下:
[code=PHP]
<?php
$file = 'test_img.bmp';
很显然,您的 <?php 前有一个空行
更低级的就是你连 BOM 头都没有避讳