PHP DOWNLOAD 下载 HEADER
PHP页面点击按钮之后生成一个CSV文件,然后提供用户下载,想做成直接下载,不用再生成一个链接,找了找资料,发现办法不少 www.shengfang.org
<?php
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="sf2.csv"');
readfile('sf2.csv');
?> www.shengfang.org
这个需要 HEADER放在最前面,需要作一个新的PHP文件
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Location:http://127.0.0.1/bill/admin/test/sf2.csv"); www.shengfang.org
这个比较简单,直接导向一个超级链接,让IE去处理。防制瀏覽器讀取Cache的Header用法
能在echo ,<html> ,session ,space后面出现的header:
内容:
问:我用header("location:文件名")导向这个文件,为什么总是提示我" Cannot add header information - headers already sent by "这个信息,请高手指点!在此表示感谢!
答:用這個代替header
header 調用之前,不能輸出http 頭。如果在之前輸出,echo ,<html> ,session ,space都會出現錯誤。
下面的function 無此限制 www.shengfang.org
function redirectURL($url, $seconds = 0){
/*can replace the head() function
at para $url eg:$url='welcome dot php?sid=$sid|3';
redirectURL($url); //3 s, redirect $url
*/
if(!is_int($seconds))
$seconds=0;
$mta = "<META HTTP-EQUIV=REFRESH CONTENT=\"".$seconds.
";URL=".$url."\">";
echo $mta;
exit;
} www.shengfang.org
echo "sdffffffff";
redirectURL("http://127.0.0.1/bill/admin/test/sf2.rar");
exit();
If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.
<?php www.shengfang.org
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?> www.shengfang.org
注: There is a bug in Microsoft Internet Explorer 4.01 that prevents this from working. There is no workaround. There is also a bug in Microsoft Internet Explorer 5.5 that interferes with this, which can be resolved by upgrading to Service Pack 2 or later.
注: If safe mode is enabled the uid of the script is added to the realm part of the WWW-Authenticate header if you set this header (used for HTTP Authentication).
header www.shengfang.org
(PHP 3, PHP 4, PHP 5) www.shengfang.org
header -- Send a raw HTTP header
Description
void header ( string string [, bool replace [, int http_response_code]] )
header() is used to send raw HTTP headers. See the HTTP/1.1 specification for more information on HTTP headers.