2012-02-27 105 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