2012-02-27 103 views
3

所以我有,你点击一个链接一个下载页面,它会打开/下载/下载/ randomhash头位置+内容处理

randomhash在数据库中,我增加一个下载计数器,然后重定向到实际文件,例如/uploads/2012/file.png。

一切工作,除了重定向做我想做的事情。我不知道为什么它不工作...

header("Location: " . $row->uri); 
    header("Content-Disposition: attachment; filename=$row->name"); 

在文件的第一个负载,它具有适当的内容处理标头(萤火虫),但它并没有提示该文件是下载(它应该,对吗?)。有任何想法吗?

响应头:

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, public 
Connection: Keep-Alive 
Content-Disposition: attachment; filename=promotion_photo_2.jpg 
Content-Encoding: gzip 
Content-Length: 20 
Content-Type: text/html; charset=utf-8 
Date: Mon, 27 Feb 2012 01:01:22 GMT 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Keep-Alive: timeout=5, max=100 
Location: /uploads/2012/mediakitD3CF.jpg 
Pragma: no-cache 
Server: * 
Vary: Accept-Encoding 
X-Powered-By: * 
X-UA-Compatible: IE=Edge,chrome=1 
+1

你应该重定向到一个下载控制器将处理相应的头文件,目前你只需添加内容处理标头的重定向头 – 2012-02-27 01:08:00

回答

4

您在设置中告诉浏览器重定向到相同的响应内容处置头。我的建议是刚刚流对响应的附件,没有重定向

header('Content-Disposition: attachment; filename=file-to-be-downloaded.jpg'); 
header('Content-type: image/jpeg'); // or what is relevant, ie application/octet-stream 
$fn=fopen("path-to-file/file-to-be-downloaded.jpg","r"); 
fpassthru($fn); 
+2

如何将与〜300MB的电影文件:\?我最初有类似于这个使用file_get_contents但PHP运行内存试图缓冲300mb的内容到浏览器......大声笑。我的另一个想法是有条件地在我的apache配置中设置上传任何内容的标题,但不知道如何做到这一点(不能做'RewriteCond'和标题集或FilesMatch ...) – Benno 2012-02-27 01:18:35

+0

抱歉,你只是把一个JPG图像在你的问题;对于更大的下载,请阅读第4条评论:http://php.net/manual/en/function.fpassthru.php或使用http://pear.php.net/manual/en/package.http.http-download .intro.php – 2012-02-27 01:27:51

+0

你真的建议用fpassthru通过你的网络服务器代理整个文件?这不仅可能会阻止您阻止,但它可能会从您的网站的分配带宽中扣除。 – NoBugs 2018-01-19 04:39:00