标题位置+内容配置

Ben*_*nno 5 php content-disposition http-headers

所以我有一个下载页面,您单击链接,它会打开 /downloads/download/randomhash

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

除了重定向做我想做的事情之外,一切正常。我不知道为什么它不起作用......

  header("Location: " . $row->uri);
  header("Content-Disposition: attachment; filename=$row->name");
Run Code Online (Sandbox Code Playgroud)

在第一次加载文件时,它具有适当的内容处置标头(在 firebug 中),但它不会提示下载文件(它应该是这样,对吧??)。有任何想法吗?

响应标头:

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
Run Code Online (Sandbox Code Playgroud)

gui*_*ido 4

您在同一响应中设置 Content-Disposition 标头,告诉浏览器重定向到哪里。我的建议是仅在响应中传输附件,而不进行重定向

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);
Run Code Online (Sandbox Code Playgroud)

  • 大约 300mb 的电影文件将如何处理:\?我最初使用 file_get_contents 有类似的东西,但 PHP 试图将 300mb 的内容缓冲到浏览器时内存不足......哈哈。我的另一个想法是在我的 apache 配置中为上传中的任何内容有条件地设置标头,但不确定如何执行此操作(无法执行“RewriteCond”和标头集或 FilesMatch...) (2认同)