PHP文件下载奇怪的错误
我在通过PHP下载文件时出错 守则:PHP文件下载奇怪的错误,php,file,download,rtf,Php,File,Download,Rtf,我在通过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-
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)上测试
还尝试使用作为mime类型:application/force download
,但运气不佳
错误:
我在Chrome中遇到一个错误:
此网页不可用
在Firefox中:
页面未正确重定向
Firefox检测到服务器正在以一种永远不会完成的方式重定向对此地址的请求。
您需要为.rtf和.txt文件添加内容类型
首先检查文件扩展名,然后输入
否则,请设置.rtf和.txt文件的内容类型
干杯:)
我想这会帮助你找到所有的内容类型
此代码解决了以下问题:
if (file_exists($myFile)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($myFile));
readfile($myFile);
exit;
}
来源:对您来说“不工作”是什么意思?旁注,也可以使用
filename=“”.basename($filename)。“
进行了编辑,描述了我遇到的错误getting@LozCheroneツ 感谢您的提醒,您的错误不在下载脚本中。循环重定向是由其他原因引起的。检查有重定向的代码。