先給大家介紹下PHP header() 函數
定義和用法
header() 函數向客戶端發送原始的 HTTP 報頭。
認識到一點很重要,即必須在任何實際的輸出被發送之前調用 header() 函數(在 PHP 4 以及更高的版本中,您可以使用輸出緩存來解決此問題):
<html><?php// 結果出錯// 在調用 header() 之前已存在輸出header('Location: http://www.example.com/');?>
語法
header(string,replace,http_response_code)
數 | 描述 |
---|---|
string | 必需。規定要發送的報頭字符串。 |
replace | 可選。指示該報頭是否替換之前的報頭,或添加第二個報頭。 默認是 true(替換)。false(允許相同類型的多個報頭)。 |
http_response_code | 可選。把 HTTP 響應代碼強制為指定的值。(PHP 4 以及更高版本可用) |
php文件下載可以使用http的請求頭加上php的IO可以實現,很久之前寫過這么一個功能,后來代碼沒了,今天記錄一下
1、先看一下一個正常的http請求
HTTP/1.1 200 OKServer: TengineContent-Type: application/octet-streamContent-Length: 5050697Connection: keep-aliveDate: Thu, 12 Oct 2017 11:24:46 GMTAccept-Ranges: bytesContent-Disposition: attachment; filename=down/20170928/zjbb_2.9.5.apkExpires: Thu, 12 Oct 2017 11:25:46 GMTCache-Control: max-age=60Via: cache25.l2eu6-1[0,200-0,H], cache16.l2eu6-1[16,0], cache8.cn891[0,200-0,H], cache8.cn891[1,0]Age: 1733678X-Cache: HIT TCP_MEM_HIT dirn:6:277104755 mlen:-1X-Swift-SaveTime: Sat, 14 Oct 2017 00:50:47 GMTX-Swift-CacheTime: 93312000Timing-Allow-Origin: *EagleId: b73d0e1c15095411645886178e
2、一些常見的header功能
header('HTTP/1.1 200 OK'); // ok 正常訪問header('HTTP/1.1 404 Not Found'); //通知瀏覽器 頁面不存在header('HTTP/1.1 301 Moved Permanently'); //設置地址被永久的重定向 301header('Location: http://www.test.con/'); //跳轉到一個新的地址header('Refresh: 10; url=http://www.test.con/'); //延遲轉向 也就是隔幾秒跳轉header('X-Powered-By: PHP/7.0.0'); //修改 X-Powered-By信息header('Content-language: en'); //文檔語言header('Content-Length: 1234'); //設置內容長度header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); //告訴瀏覽器最后一次修改時間header('HTTP/1.1 304 Not Modified'); //告訴瀏覽器文檔內容沒有發生改變###內容類型###header('Content-Type: text/html; charset=utf-8'); //網頁編碼header('Content-Type: text/plain'); //純文本格式header('Content-Type: image/jpeg'); //JPG、JPEGheader('Content-Type: application/zip'); // ZIP文件header('Content-Type: application/pdf'); // PDF文件header('Content-Type: audio/mpeg'); // 音頻文件header('Content-type: text/css'); //css文件header('Content-type: text/javascript'); //js文件header('Content-type: application/json'); //jsonheader('Content-type: application/pdf'); //pdfheader('Content-type: text/xml'); //xmlheader('Content-Type: application/x-shockw**e-flash'); //Flash動畫#########聲明一個下載的文件###header('Content-Type: application/octet-stream');header('Content-Disposition: attachment; filename="ITblog.zip"');header('Content-Transfer-Encoding: binary');readfile('test.zip');#########對當前文檔禁用緩存###header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');#########顯示一個需要驗證的登陸對話框###header('HTTP/1.1 401 Unauthorized');header('WWW-Authenticate: Basic realm="Top Secret"');#########聲明一個需要下載的xls文件###header('Content-Disposition: attachment; filename=abc.xlsx');header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');header('Content-Length: '.filesize('./test.xls'));header('Content-Transfer-Encoding: binary');header('Cache-Control: must-revalidate');header('Pragma: public');readfile('./test.xls');
3、看下下載所要用的的請求頭
header("Content-type:application/octet-stream");header("Accept-Ranges:bytes");header("Accept-Length:".$file_Size);header("Content-Disposition: attachment; filename=".$filename);
4、php的文件操作出現的比較早,文件名是中文的時候需要注意轉碼
$filename=iconv("UTF-8","GB2312",$filename);
5、php的文件下載機制是首先nginx把文件信息讀入服務器內存,然后使用請求頭把文件二進制信息通過瀏覽器傳給客戶端
feof用來判斷文件是否已經讀到了末尾,fread用來把文件讀入緩沖區,緩沖區的大小是1024,一邊讀取一邊把數據輸出到瀏覽器。為了下載的安全性每次讀數據都進行字節的計數。文件讀取完畢后關閉輸入流
注意:
a、如果運行的過程中出現問題,可以清空(擦掉)輸出緩沖區,使用下面的代碼即可
ob_clean();
b、很多人喜歡用readfile,如果是大文件,可能會有問題
完整代碼
<?php ob_clean(); $action = $_GET['action']; $filename = base64_decode($action);//傳的參數encode了 $filepath = '/data/www/www.test.com/'.$filename; if(!file_exists($filepath)){ exit; } $fp=fopen($filepath,"r"); $filesize=filesize($filepath); header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes"); header("Accept-Length:".$filesize); header("Content-Disposition: attachment; filename=".$filename); $buffer=1024; $buffer_count=0; while(!feof($fp)&&$file_Size-$buffer_count>0){ $data=fread($fp,$buffer); $buffer_count+=$buffer; echo $data; } fclose($fp);?>
PS:下面看一段實例代碼php如何通過header文件頭實現文件下載
具體代碼如下所示:
$file = $_GET['file'];if(file_exists($file)){header("Content-type:application/octet-stream");$filename = basename($file);header("Content-Disposition:attachment;filename = ".$filename);header("Accept-ranges:bytes");header("Accept-length:".filesize($file));readfile($file);}else{ echo "<script>alert('文件不存在')</script>";}
總結
以上所述是小編給大家介紹的PHP使用header方式實現文件下載功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
新聞熱點
疑難解答
圖片精選