如何在 PHP 中自动开始下载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40943/
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-24 21:17:03  来源:igfitidea点击:

How to Automatically Start a Download in PHP?

phpautomationdownload

提问by kaybenleroll

What code do you need to add in PHP to automatically have the browser download a file to the local machine when a link is visited?

需要在PHP中添加什么代码才能在访问链接时自动让浏览器将文件下载到本地机器?

I am specifically thinking of functionality similar to that of download sites that prompt the user to save a file to disk once you click on the name of the software?

我特别在想类似于下载站点的功能,一旦您单击软件名称,就会提示用户将文件保存到磁盘?

回答by Robert Swisher

Send the following headers before outputting the file:

在输出文件之前发送以下标头:

header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($File));
header("Connection: close");

@grom: Interesting about the 'application/octet-stream' MIME type. I wasn't aware of that, have always just used 'application/force-download' :)

@grom:关于“应用程序/八位字节流”MIME 类型很有趣。我不知道,一直只使用“应用程序/强制下载”:)

回答by grom

Here is an example of sending back a pdf.

这是发回pdf的示例。

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
header('Content-Transfer-Encoding: binary');
readfile($filename);

@SwishI didn't find application/force-download content type to do anything different (tested in IE and Firefox). Is there a reason for not sending back the actual MIME type?

@Swish我没有找到应用程序/强制下载内容类型来做任何不同的事情(在 IE 和 Firefox 中测试)。是否有理由不发回实际的 MIME 类型?

Also in the PHP manual Hayley Watsonposted:

同样在 PHP 手册中,Hayley Watson发布了:

If you wish to force a file to be downloaded and saved, instead of being rendered, remember that there is no such MIME type as "application/force-download". The correct type to use in this situation is "application/octet-stream", and using anything else is merely relying on the fact that clients are supposed to ignore unrecognised MIME types and use "application/octet-stream" instead (reference: Sections 4.1.4 and 4.5.1 of RFC 2046).

如果您希望强制下载和保存文件,而不是呈现文件,请记住没有像“application/force-download”这样的 MIME 类型。在这种情况下使用的正确类型是“application/octet-stream”,而使用其他任何类型仅依赖于客户端应该忽略无法识别的 MIME 类型并使用“application/octet-stream”这一事实(参考:部分RFC 2046 的 4.1.4 和 4.5.1)。

Also according IANAthere is no registered application/force-download type.

此外,根据IANA,没有注册的应用程序/强制下载类型。

回答by vdbuilder

A clean example.

一个干净的例子。

<?php
    header('Content-Type: application/download');
    header('Content-Disposition: attachment; filename="example.txt"');
    header("Content-Length: " . filesize("example.txt"));

    $fp = fopen("example.txt", "r");
    fpassthru($fp);
    fclose($fp);
?>

回答by Omidoo

my code works for txt,doc,docx,pdf,ppt,pptx,jpg,png,zip extensions and I think its better to use the actual MIME types explicitly.

我的代码适用于 txt、doc、docx、pdf、ppt、pptx、jpg、png、zip 扩展名,我认为最好明确使用实际的 MIME 类型。

$file_name = "a.txt";

// extracting the extension:
$ext = substr($file_name, strpos($file_name,'.')+1);

header('Content-disposition: attachment; filename='.$file_name);

if(strtolower($ext) == "txt")
{
    header('Content-type: text/plain'); // works for txt only
}
else
{
    header('Content-type: application/'.$ext); // works for all extensions except txt
}
readfile($decrypted_file_path);