【发布时间】:2014-09-09 14:34:35
【问题描述】:
通过 PHP 下载文件时出现错误。
代码:
if(file_exists($myFile)) {
$mm_type="application/octet-stream";
header("Cache-Control: public, must-revalidate");
header("Pragma: hack"); // WTF? oh well, it works...
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($myFile)) );
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header("Content-Transfer-Encoding: binary\n");
$fp = fopen($myFile, 'rb');
$buffer = fread($fp, filesize($myFile));
fclose ($fp);
print $buffer;
}
以上代码适用于 doc 文件、pdf 文件、zip 文件、jpg/png 文件,
但当我尝试下载 .rtf 或 .txt 文件时失败。
在所有主流浏览器(Chrome、FF、IE、Safari)上测试。
还尝试使用 as mime-type : application/force-download 没有运气。
错误:
我在 Chrome 中遇到错误:
This webpage is not available
在 Firefox 中:
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
【问题讨论】:
-
对你来说“不工作”是什么意思?
-
旁注,也可以使用
filename="'.basename($filename).'" -
进行了编辑,描述了我遇到的错误
-
@LozCheroneツ 感谢您的来信
-
您的错误不在您的下载脚本中。循环重定向是由其他原因引起的。检查你有重定向的代码。