php:强制下载到高清? [英] php: force download to hd?

查看:96
本文介绍了php:强制下载到高清?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();

任何想法我做错了什么?这不应该从我的服务器下载任何文件到用户的硬盘?不知何故每个文件都被损坏!此外,我想知道如何更改下载文件的文件名?

Any idea what I'm doing wrong? Shouldn't this download any file from my server to the harddrive of the user? Somehow every file is damaged! Moreover I wonder how I can change the filename of the downloaded file?

$ file 始终包含完整路径我的档案
如果我尝试标头('位置:'。$文件); 浏览器成功打开我的文件。但是如果文件是.jpg,浏览器不会提示下载窗口。而只是在浏览器窗口中打开文件。我希望每个文件都被下载到hd。

$file always contains the full path to my file. if I try header('Location:' . $file ); the browser successfully opens my file. however if the file is a .jpg the browser doesn't prompt the download window. instead it just opens the file in the browserwindow. I want every file to be downloaded to the hd.

请帮助我。我现在已经过了一个多星期了,我找不到解决方案?

Please help me guys. I'm after this for over a week now and I can't find a solution?

推荐答案

为什么不使用

header(Content-type:application / octet-stream);

而不是强制下载

另外你为什么要做 file_get_contents ReadFile的?只需要一个 - 你基本上包含文件两次,这就是为什么它被破坏。这是我如何执行上述代码:

Also why are you doing file_get_contents and readfile? Only one is needed - You are basically including the file twice which is why it's corrupted. Here is how I would execute the above code:

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);

这应该是足够的 - 只要文件实际存在

That should be sufficient - so long as the file actually exists

这篇关于php:强制下载到高清?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆