2009-07-02 82 views
2

我試圖創建可下載的視頻文件。在我的網站上有一個文件列表。 所有視頻均爲.flv格式(flash)。所有視頻都有精確的文件鏈接。 但是在點擊內容之後的所有瀏覽器中都會加載到瀏覽器的窗口。我不需要這個。據我瞭解,我應該創建包含MIME類型的下載文件的重定向頁面。我該做什麼? 語言:PHPMIME類型的下載文件

回答

7

用下面的命令創建一個PHP頁面:

<?php 

$filepath = "path/to/file.ext"; 

header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-Disposition: attachment; filename=$filepath"); 
header("Content-Type: mime/type"); 
header("Content-Transfer-Encoding: binary"); 
// UPDATE: Add the below line to show file size during download. 
header('Content-Length: ' . filesize($filepath)); 

readfile($filepath); 

?> 

設置$filepath到文件要下載的路徑,並設置Content-Type到要下載的文件的MIME類型。

將「下載」鏈接指向此頁面。

對於同一類型的多個文件:

<?php 

$filepath = $_GET['filepath']; 

header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-Disposition: attachment; filename=$filepath"); 
header("Content-Type: mime/type"); 
header("Content-Transfer-Encoding: binary"); 
// UPDATE: Add the below line to show file size during download. 
header('Content-Length: ' . filesize($filepath)); 

readfile($filepath); 

?> 

更換信息作爲上述規定,並與名爲「文件路徑」包含文件路徑的GET參數點「下載」鏈接到這個網頁。例如,如果您將該php文件命名爲「download.php」,請將名爲「movie.mov」(與download.php位於同一目錄中)的文件的下載鏈接指向「download.php?filepath = movie.mov」。

+0

正如我所知download.php?filepath = movie.mov不好=)導致everyboady可以從服務器上下載任何文件。不過謝謝。 – Ockonal 2009-07-02 17:04:47

9

推薦的MIME類型即application/octet-stream

的「八位字節流」子類型用於指示一個主體包含任意的二進制數據。 [...]

接收「application/octet-stream」實體的實現的推薦操作是僅提供將數據放入文件中,並取消任何Content-Transfer-Encoding,或者可能使用它作爲用戶指定進程的輸入。