移动设备的HTTP标题“Content-Type:application / force-download”的效用?

时间:2012-05-16 09:34:53

标签: php http mobile download content-type

我目前正在开发一个PHP脚本,允许您通过访问链接从移动设备下载媒体内容(视频,音频,图片...)。 (即http://www.my-web-site.com/download.php?id=7ejs8ap) 当我使用最近的手机(三星Galaxy S,iPhone 4S,其他一些......)进行测试时,我的脚本非常有用,但我的旧手机三星C3050出现了错误。我想下载的媒体只是一个我经常下载的音频mp3文件。

错误似乎是“未知内容类型”。 因此,由于我唯一的HTTP标头Content-Type是“application / force-download”,我尝试对此进行评论并再试一次。然后,它的工作原理。 但是现在,我目前正在询问这种内容类型的含义以及是否可以强制其他移动设备。我在iPhone 4上没有使用Content-Type进行测试但是它可以工作,但我不确定所有移动设备的兼容性。

有人可以解释一下Content-Type是如何工作的,为什么这不是标准的MIME或其他所有可以帮助我确保这是每个下载的选项内容类型,无论文件,浏览器或我正在下载的设备?

谢谢大家。

这是我发送的PHP标头:

<?php
//Assume that $filename and $filePath are correclty set.
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$filename.'"');
// header('Content-Type: application/force-download'); Non-standard MIME-Type, incompatible with Samsung C3050 for example. Let it commented
readfile($filePath);
?>
编辑:我刚尝试过索尼Xperia,下载不成功:我只看到我要下载的文件的“html编码”字节。 如果application / octet-stream或application / force-download不起作用,我怎么知道我必须使用哪种内容类型?

3 个答案:

答案 0 :(得分:111)

Content-Type: application/force-download表示“我,网络服务器,我会欺骗你(浏览器)关于这个文件是什么,所以你不会把它当作PDF / Word文档/ MP3 /其他和提示用户将神秘文件保存到磁盘“。当客户端没有“保存到磁盘”时,这是一个可怕的破解。

对于您正在使用的任何媒体使用正确的mime类型(例如,audio/mpeg用于mp3)。

如果您希望鼓励客户端下载它而不是遵循默认行为,请使用Content-Disposition: attachment; etc etc标头。

答案 1 :(得分:14)

要下载文件,请使用以下代码...将文件名与位置存储在$ file变量中。它支持所有mime类型

$file = "location of file to download"
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);

要了解Mime类型,请参阅此链接:http://php.net/manual/en/function.mime-content-type.php

答案 2 :(得分:4)

application/force-download不是标准的MIME类型。这是最近添加的一些浏览器支持的黑客攻击。

你的问题没有任何意义。这就像问为什么Internet Explorer 4不支持最新的CSS 3功能。

相关问题