使用 header() 强制使用 php 下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8485886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:50:19  来源:igfitidea点击:

Force file download with php using header()

phphttp-headers

提问by Kerp

I want the user to be able to download some files I have on my server, but when I try to use any of the many examples of this around the internet nothing seems to work for me. I've tried code like this:

我希望用户能够下载我的服务器上的一些文件,但是当我尝试使用互联网上的许多示例中的任何一个时,似乎没有什么对我有用。我试过这样的代码:

<?php

$size = filesize("Image.png");

header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="Image.png"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
readfile("Image.png");

I've even tried to use the most basic example I could find, like this:

我什至尝试使用我能找到的最基本的示例,如下所示:

<?php
header('Content-type: image/png');
header('Content-Disposition: attachment; filename="Image.png"');
readfile('Image.png');

When I've tested this I have removed all the other code I have and used an empty file with just this code to remove any faults created by external sources.

当我对此进行测试时,我已经删除了我拥有的所有其他代码,并使用仅包含此代码的空文件来删除由外部源创建的任何故障。

When I look in the console the file gets sent with the right headers i.e

当我查看控制台时,文件会以正确的标题发送,即

'Content-Disposition: attachment; filename="Image.png"'

But the save dialog isn't displayed.

但不显示保存对话框。

I've also tried with inline instead of attachment in the content disposition header but that didn't make a difference either, I've tested this in Firefox 8.0.1 Chrome 15.0.874.121 and Safari 5.1.1.

我也试过在内容处理标头中使用内联而不是附件,但这也没有什么区别,我已经在 Firefox 8.0.1 Chrome 15.0.874.121 和 Safari 5.1.1 中测试过这个。

采纳答案by Kerp

The problem was that I used ajax to post the message to the server, when I used a direct link to download the file everything worked fine.

问题是我使用ajax将消息发布到服务器,当我使用直接链接下载文件时一切正常。

I used this other Stackoverflow Q&A material instead, it worked great for me:

我使用了另一种 Stackoverflow 问答材料,它对我很有用:

回答by Lawrence Cherone

I'm pretty sure you don't add the mime type as a JPEG on file downloads:

我很确定您不会在文件下载时将 mime 类型添加为 JPEG:

header('Content-Type: image/png');

These headers have never failed me:

这些标题从来没有让我失望:

$quoted = sprintf('"%s"', addcslashes(basename($file), '"\'));
$size   = filesize($file);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted); 
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);

回答by Farhan Sahibole

This worked for me like a charm for downloading PNG and PDF.

这对我来说就像下载 PNG 和 PDF 的魅力一样。

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file_url)); //Absolute URL
ob_clean();
flush();
readfile($file_url); //Absolute URL
exit();

回答by Robin van den Hurk

Based on Farhan Sahibole's answer:

基于Farhan Sahibole 的回答

        header('Content-Disposition: attachment; filename=Image.png');
        header('Content-Type: application/octet-stream'); // Downloading on Android might fail without this
        ob_clean();

        readfile($file);

This is all I needed for this to work. I stripped off anything that isn't required for this to work.

这就是我所需要的一切。我去掉了任何不需要的东西。

Key is to use ob_clean();

关键是用 ob_clean();

回答by Mhmd

its work for me

它对我有用

$attachment_location = "filePath";
if (file_exists($attachment_location)) {

    header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
    header("Cache-Control: public"); // needed for internet explorer
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: Binary");
    header("Content-Length:".filesize($attachment_location));
    header("Content-Disposition: attachment; filename=filePath");
    readfile($attachment_location);
    die();
} else {
    die("Error: File not found.");
}