该脚本适用于我的 http 文件服务器。我的文件来自 web 根目录,所以我使用 php 脚本来抓取文件并将其发送到客户端。问题是,当我单击文件下载浏览器(ff 和 chrome)时,不会询问我是否要保存文件。在 chrome 的网络工具中,在网络下,我看到 download.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