2012-12-01 40 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

相关问题