在php中强制下载
我正在尝试创建一个强制下载页面,以防止浏览器打开应该下载的文件。问题是下载的文件有0字节,因此无法使用。我的代码有什么问题吗?
$file = "http://gh0stsec.zxq.net/background1.jpg";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
header("Content-Description: File Transfer");
@readfile($file);
exit();
I am trying to create a force-download page to prevent browsers from opening files that should be downloaded. The problem is that the downloaded file has 0 bytes and thus is unusable. What is wrong with my code?
$file = "http://gh0stsec.zxq.net/background1.jpg";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
header("Content-Description: File Transfer");
@readfile($file);
exit();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查:
我认为应该是:
check:
I think it should be:
我认为您会遇到
filesize
问题,因为它不是本地文件(如果是,请使用相对路径)。Content-Type
标头对我来说总是有点奇怪,但在我读到的所有示例中,force-download
始终是一种后备。无论如何,我这样做了,它似乎有效:I think you're going to have an issue with
filesize
since it's not a local file (if it is, use a relative path).Content-Type
headers are always a little weird for me, but in all of the examples I read,force-download
was always a fallback. Anyways, I did this and it seemed to work: