php 使用浏览器提示下载文件

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

using the browser prompt to download a file

phpmysqlbrowserdownloadprompt

提问by Ed Cohen

I have a php/mysql site in which I am trying to download a comma delimited file (CSV). The csv file I create contains comma delimited data (name, address, city, state). I create the csv file ok and place it in the site's /downloads directory. So far so good. I have been looking on line and the code to trigger the browser's download prompt that I see the most often is:

我有一个 php/mysql 站点,我试图在其中下载逗号分隔文件 (CSV)。我创建的 csv 文件包含逗号分隔的数据(名称、地址、城市、州)。我创建了 csv 文件,并将其放在站点的 /downloads 目录中。到现在为止还挺好。我一直在网上找,我最常看到的触发浏览器下载提示的代码是:

$path = $_SERVER['DOCUMENT_ROOT'];
$exportfile = "emailclientaddresses.csv";
$fullpath = "downloads/" . $exportfile;
header("Content-type: text/plain");
header("Content-Length: ".filesize($exportfile));
header("Content-Disposition: attachment; filename=" . $fullpath);

The $exportfile is the csv file that my code created. It's ok. What this does is:

$exportfile 是我的代码创建的 csv 文件。没关系。它的作用是:

  1. The $fullpath is displayed in the browser download prompt in a very weird format: download_emailclientaddresses.csv
  2. When it does download, the current webpage is downloaded or a combination of the csv file and the current web page.
  1. $fullpath 以一种非常奇怪的格式显示在浏览器下载提示中:download_emailclientaddresses.csv
  2. 下载时,会下载当前网页或 csv 文件和当前网页的组合。

OK, I have tried a lot of things and nothing has worked. So, if anyone can help me I would appreciate it. Thank you.

好吧,我已经尝试了很多东西,但没有任何效果。所以,如果有人能帮助我,我将不胜感激。谢谢你。

ed Cohen

埃德·科恩

回答by Anne

The PHP documentationprovides a nice example:

PHP文档提供了一个很好的例子:

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

EDIT(Response to comment, explanation)

编辑(对评论的回应,解释)

header('Content-Description: File Transfer');

Do not display in the browser, but transfer the file.

不在浏览器中显示,而是传输文件。

header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');

File is a binary file.
Browsers generally download binary files, unless they can display them.

文件是一个二进制文件。
浏览器通常下载二进制文件,除非它们可以显示它们。

header('Content-Disposition: attachment; filename='.basename($file));

Make the download dialog show the proper file name.
Note: You can use any file name.

使下载对话框显示正确的文件名。
注意:您可以使用任何文件名。

header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');

File should not be cached by the browser.
Cache could cause trouble in case of dynamic content.

文件不应被浏览器缓存。
如果是动态内容,缓存可能会导致问题。

header('Content-Length: ' . filesize($file));

Send the correct file size to the browser,
otherwise the browser is unable to estimate the transfer-time.

将正确的文件大小发送到浏览器,
否则浏览器无法估计传输时间。

ob_clean();
flush();

Make sure the headers are send to the browser before the download starts.

确保在下载开始之前将标题发送到浏览器。

readfile($file);

Send the file to the browser.

将文件发送到浏览器。

exit;

Done :)

完毕 :)