使用浏览器提示下载文件 [英] using the browser prompt to download a file

查看:167
本文介绍了使用浏览器提示下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

$ exportfile是我的代码创建的csv文件。没关系。这样做是:

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


  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

推荐答案

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

The PHP documentation provides a nice example:

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

将文件发送到浏览器。

exit;

完成:)

这篇关于使用浏览器提示下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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