簡體   English   中英

Mime類型的下載文件

[英]Mime-type of downloading file

我正在嘗試創建可下載的視頻文件。 在我的網站中有一個文件列表。 所有視頻均采用.flv格式(閃光燈)。 所有視頻的文件都有確切的鏈接。 但是在點擊內容后的所有瀏覽器中都會加載到瀏覽器的窗口中。 我不需要這個。 據我所知,我應該創建redirect-page包含mime-type的下載文件。 我該怎么辦? 語言:php

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

“八位字節流”子類型用於指示正​​文包含任意二進制數據。 [...]

接收“application / octet-stream”實體的實現的建議操作是簡單地提供將數據放入文件中,其中任何Content-Transfer-Encoding撤消,或者可能將其用作用戶指定的輸入處理。

使用以下內容創建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設置$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);

?>

替換上面指定的信息,並使用包含文件路徑的名為“filepath”的GET參數指向此頁面的“下載”鏈接。

例如,如果您將此php文件命名為“download.php”,請將名為“movie.mov”的文件(與download.php位於同一目錄中)的下載鏈接指向“download.php?filepath = movie.mov” 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM