【发布时间】:2010-11-07 16:13:08
【问题描述】:
我正在尝试创建可下载的视频文件。在我的网站上有一个文件列表。 所有视频均为 .flv 格式(闪存)。所有视频都有指向该文件的确切链接。 但是在所有浏览器中单击内容后都会加载到浏览器的窗口中。我不需要这个。据我了解,我应该创建包含下载文件的 mime 类型的重定向页面。我究竟应该怎么做? 语言:php
【问题讨论】:
我正在尝试创建可下载的视频文件。在我的网站上有一个文件列表。 所有视频均为 .flv 格式(闪存)。所有视频都有指向该文件的确切链接。 但是在所有浏览器中单击内容后都会加载到浏览器的窗口中。我不需要这个。据我了解,我应该创建包含下载文件的 mime 类型的重定向页面。我究竟应该怎么做? 语言:php
【问题讨论】:
使用以下内容创建一个 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);
?>
替换上面指定的信息,并使用包含文件路径的名为“filepath”的 GET 参数将“下载”链接指向此页面。
例如,如果您将此 php 文件命名为“download.php”,则将名为“movie.mov”的文件(与 download.php 位于同一目录中)的下载链接指向“download.php?filepath=movie” .mov”。
【讨论】:
为此推荐的 MIME 类型是 application/octet-stream:
“八位字节流”子类型用于指示主体包含任意二进制数据。 […]
对于接收“application/octet-stream”实体的实现,推荐的操作是简单地将数据放入文件中,取消任何 Content-Transfer-Encoding,或者将其用作用户指定的进程。
【讨论】: