先給大家介紹下PHP header() 函數(shù)
定義和用法
header() 函數(shù)向客戶端發(fā)送原始的 HTTP 報(bào)頭。
認(rèn)識(shí)到一點(diǎn)很重要,即必須在任何實(shí)際的輸出被發(fā)送之前調(diào)用 header() 函數(shù)(在 PHP 4 以及更高的版本中,您可以使用輸出緩存來解決此問題):
<html><?php// 結(jié)果出錯(cuò)// 在調(diào)用 header() 之前已存在輸出header('Location: http://www.example.com/');?>語法
header(string,replace,http_response_code)
| 數(shù) | 描述 |
|---|---|
| string | 必需。規(guī)定要發(fā)送的報(bào)頭字符串。 |
| replace | 可選。指示該報(bào)頭是否替換之前的報(bào)頭,或添加第二個(gè)報(bào)頭。 默認(rèn)是 true(替換)。false(允許相同類型的多個(gè)報(bào)頭)。 |
| http_response_code | 可選。把 HTTP 響應(yīng)代碼強(qiáng)制為指定的值。(PHP 4 以及更高版本可用) |
php文件下載可以使用http的請(qǐng)求頭加上php的IO可以實(shí)現(xiàn),很久之前寫過這么一個(gè)功能,后來代碼沒了,今天記錄一下
1、先看一下一個(gè)正常的http請(qǐng)求
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'); //設(shè)置地址被永久的重定向 301header('Location: http://www.test.con/'); //跳轉(zhuǎn)到一個(gè)新的地址header('Refresh: 10; url=http://www.test.con/'); //延遲轉(zhuǎn)向 也就是隔幾秒跳轉(zhuǎn)header('X-Powered-By: PHP/7.0.0'); //修改 X-Powered-By信息header('Content-language: en'); //文檔語言header('Content-Length: 1234'); //設(shè)置內(nèi)容長(zhǎng)度header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); //告訴瀏覽器最后一次修改時(shí)間header('HTTP/1.1 304 Not Modified'); //告訴瀏覽器文檔內(nèi)容沒有發(fā)生改變###內(nèi)容類型###header('Content-Type: text/html; charset=utf-8'); //網(wǎng)頁編碼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動(dòng)畫#########聲明一個(gè)下載的文件###header('Content-Type: application/octet-stream');header('Content-Disposition: attachment; filename="ITblog.zip"');header('Content-Transfer-Encoding: binary');readfile('test.zip');#########對(duì)當(dāng)前文檔禁用緩存###header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');#########顯示一個(gè)需要驗(yàn)證的登陸對(duì)話框###header('HTTP/1.1 401 Unauthorized');header('WWW-Authenticate: Basic realm="Top Secret"');#########聲明一個(gè)需要下載的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、看下下載所要用的的請(qǐng)求頭
header("Content-type:application/octet-stream");header("Accept-Ranges:bytes");header("Accept-Length:".$file_Size);header("Content-Disposition: attachment; filename=".$filename);4、php的文件操作出現(xiàn)的比較早,文件名是中文的時(shí)候需要注意轉(zhuǎn)碼
$filename=iconv("UTF-8","GB2312",$filename);5、php的文件下載機(jī)制是首先nginx把文件信息讀入服務(wù)器內(nèi)存,然后使用請(qǐng)求頭把文件二進(jìn)制信息通過瀏覽器傳給客戶端
feof用來判斷文件是否已經(jīng)讀到了末尾,fread用來把文件讀入緩沖區(qū),緩沖區(qū)的大小是1024,一邊讀取一邊把數(shù)據(jù)輸出到瀏覽器。為了下載的安全性每次讀數(shù)據(jù)都進(jìn)行字節(jié)的計(jì)數(shù)。文件讀取完畢后關(guān)閉輸入流
注意:
a、如果運(yùn)行的過程中出現(xiàn)問題,可以清空(擦掉)輸出緩沖區(qū),使用下面的代碼即可
ob_clean();
b、很多人喜歡用readfile,如果是大文件,可能會(huì)有問題
完整代碼
<?php ob_clean(); $action = $_GET['action']; $filename = base64_decode($action);//傳的參數(shù)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:下面看一段實(shí)例代碼php如何通過header文件頭實(shí)現(xiàn)文件下載
具體代碼如下所示:
$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>";}總結(jié)
以上所述是小編給大家介紹的PHP使用header方式實(shí)現(xiàn)文件下載功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VeVb武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選