2012-12-01 83 views
0

此腳本適用於我的http文件服務器。我有我的文件脫離web根目錄,所以我使用php腳本來抓取文件並將其發送到客戶端。問題是,當我點擊文件下載瀏覽器時(ff和chrome)不問我是否要保存文件。在chrome的網絡工具中,在網絡下,我看到download.php確實成功執行。事實上,我甚至可以看到文件以字節傳輸到請求的位置。但下載的文件不在客戶端計算機上。爲什麼瀏覽器客戶端詢問/下載文件?PHP下載腳本工程但客戶端瀏覽器不下載

Web服務器是Nginx。

<?php 

$path = "/trunk/"; 
$file = $_POST['filename']; 
$file_ext = pathinfo($file, PATHINFO_EXTENSION); 
$file_name = pathinfo($file, PATHINFO_BASENAME); 
$file_full = $path . $file_name; 

$finfo = new finfo(FILEINFO_MIME_TYPE); 

$ctype = $finfo -> file($file_full); 
header("Content-Disposition: attachment; filename=\"".$file_name."\""); 
header("Content-Type: " .$ctype); 
header("Content-Length: " .filesize($file_full)); 
error_log(filesize($file_full)); 

readfile($file_full); 

?> 

請求頭:

POST /php/download.php HTTP/1.1 

    Host: www.example.com 
    Connection: keep-alive 
    Content-Length: 146 
    Origin: https://www.example.com 
    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4 
    Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryMA7u5AkYPL9qGmRY 
    Accept: */* 
    Referer: https://www.example.com/index.php 
    Accept-Encoding: gzip,deflate,sdch 
    Accept-Language: en-US,en;q=0.8 
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 

響應頭

HTTP/1.1 200 OK 
Server: nginx 
Date: Sat, 01 Dec 2012 20:22:28 GMT 
Content-Type: application/zip 
Content-Length: 75090 
Connection: keep-alive 
Keep-Alive: timeout=300 
X-Powered-By: PHP/5.4.6--pl0-gentoo 
Content-Disposition: attachment; filename="try.zip" 
Expires: Sat, 01 Dec 2012 20:22:27 GMT 
Cache-Control: no-cache 

回答

0

使用內容類型:應用程序/八位字節流保證在所有瀏覽器強制下載。如果您知道您要發送二進制內容(如您的zip),請添加Content-Transfer-Encoding:binary

相關問題