我正在尝试创建可下载的视频文件.在我的网站中有一个文件列表.所有视频均采用.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);
?>
Run Code Online (Sandbox Code Playgroud)
设置$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);
?>
Run Code Online (Sandbox Code Playgroud)
替换上面指定的信息,并使用包含文件路径的名为"filepath"的GET参数指向此页面的"下载"链接.
例如,如果您将此php文件命名为"download.php",请将名为"movie.mov"的文件(与download.php位于同一目录中)的下载链接指向"download.php?filepath = movie.mov" .