1. 程式人生 > >PHP使用header方式實現文件下載

PHP使用header方式實現文件下載

transfer ati off server 動畫 shee http nginx app

php文件下載可以使用http的請求頭加上php的IO可以實現,很久之前寫過這麽一個功能,後來代碼沒了,今天記錄一下

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‘); //設置地址被永久的重定向 301
header(‘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、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‘);
######
 
###對當前文檔禁用緩存###
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);
  • content-type:文件類型

  • Accept-Ranges:表示接收數據的類型或者範圍,圖片屬於二進制的東西所以需要使用字節的方式傳輸

  • Accept-Length:表示接收的文件大小,php文件下載需要告訴瀏覽器下載的文件有多大

  • Content-Disposition:附件只需要把文件名給過去就可以,這個名稱就是下載時顯示的文件名稱

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

?>

 

PHP使用header方式實現文件下載