header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=\"". $file ."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
file_get_contents($file);
readfile($file);
exit();
知道我做错了什么吗?这不应该从我的服务器下载任何文件到用户的硬盘吗?不知怎的,每个文件都被损坏了!另外我想知道如何更改下载文件的文件名?
$file
始终包含我的文件的完整路径。
如果我尝试 header('Location:' . $file );
浏览器会成功打开我的文件。但是,如果文件是 .jpg,浏览器不会提示下载窗口。相反,它只是在浏览器窗口中打开文件。我希望每个文件都下载到硬盘。
请大家帮帮我。我已经关注这个问题一个多星期了,但找不到解决方案?
最佳答案
为什么不使用
header("内容类型:应用程序/八位字节流");
而不是“强制下载”
另外,你为什么要执行file_get_contents
和readfile
?只需要一个 - 您基本上将文件包含两次,这就是它损坏的原因。以下是我执行上述代码的方式:
header("Cache-Control: no-cache");
header("Expires: -1");
header("Content-Type: application/octet-stream;");
header("Content-Disposition: attachment; filename=\"" . basename($file) . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($file));
echo file_get_contents($file);
这应该足够了 - 只要文件实际存在
关于php:强制下载到高清?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3314579/