Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

php - using the browser prompt to download a file

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

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

  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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Send the file to the browser.

exit;

Done :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...