| 數(shù) | 描述 |
|---|---|
| string | 必需。規(guī)定要發(fā)送的報頭字符串。 |
| replace |
可選。指示該報頭是否替換之前的報頭,或添加第二個報頭。 默認是 true(替換)。false(允許相同類型的多個報頭)。 |
| http_response_code | 可選。把 HTTP 響應(yīng)代碼強制為指定的值。(PHP 4 以及更高版本可用) |
php文件下載可以使用http的請求頭加上php的IO可以實現(xiàn),很久之前寫過這么一個功能,后來代碼沒了,今天記錄一下
1、先看一下一個正常的http請求
HTTP/1.1 200 OK Server: Tengine Content-Type: application/octet-stream Content-Length: 5050697 Connection: keep-alive Date: Thu, 12 Oct 2017 11:24:46 GMT Accept-Ranges: bytes Content-Disposition: attachment; filename=down/20170928/zjbb_2.9.5.apk Expires: Thu, 12 Oct 2017 11:25:46 GMT Cache-Control: max-age=60 Via: cache25.l2eu6-1[0,200-0,H], cache16.l2eu6-1[16,0], cache8.cn891[0,200-0,H], cache8.cn891[1,0] Age: 1733678 X-Cache: HIT TCP_MEM_HIT dirn:6:277104755 mlen:-1 X-Swift-SaveTime: Sat, 14 Oct 2017 00:50:47 GMT X-Swift-CacheTime: 93312000 Timing-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è)置地址被永久的重定向 301
header('Location: http://www.test.con/'); //跳轉(zhuǎn)到一個新的地址
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)容長度
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); //告訴瀏覽器最后一次修改時間
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、JPEG
header('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'); //json
header('Content-type: application/pdf'); //pdf
header('Content-type: text/xml'); //xml
header('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');
######
###對當(dāng)前文檔禁用緩存###
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的文件操作出現(xiàn)的比較早,文件名是中文的時候需要注意轉(zhuǎn)碼
$filename=iconv("UTF-8","GB2312",$filename);
5、php的文件下載機制是首先nginx把文件信息讀入服務(wù)器內(nèi)存,然后使用請求頭把文件二進制信息通過瀏覽器傳給客戶端
feof用來判斷文件是否已經(jīng)讀到了末尾,fread用來把文件讀入緩沖區(qū),緩沖區(qū)的大小是1024,一邊讀取一邊把數(shù)據(jù)輸出到瀏覽器。為了下載的安全性每次讀數(shù)據(jù)都進行字節(jié)的計數(shù)。文件讀取完畢后關(guān)閉輸入流
注意:
a、如果運行的過程中出現(xiàn)問題,可以清空(擦掉)輸出緩沖區(qū),使用下面的代碼即可
ob_clean();
b、很多人喜歡用readfile,如果是大文件,可能會有問題
完整代碼
?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:下面看一段實例代碼php如何通過header文件頭實現(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方式實現(xiàn)文件下載功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
標(biāo)簽:南寧 樂山 定西 十堰 六安 佛山 迪慶 海南
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP使用header方式實現(xiàn)文件下載功能》,本文關(guān)鍵詞 PHP,使用,header,方式,實現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。