php 用于移动设备的 HTTP 标头“内容类型:应用程序/强制下载”的实用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10615797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:38:04  来源:igfitidea点击:

Utility of HTTP header "Content-Type: application/force-download" for mobile?

phphttpmobiledownloadcontent-type

提问by niconoe

I am currently working on a PHP script that allows you to download media contents (video, audio, pictures...) from your mobile device by accessing a link. (i.e. http://www.my-web-site.com/download.php?id=7ejs8ap) My script worked very vell when I was testing it with recent mobile (Samsung Galaxy S, iPhone 4S, some others...) but an error occured on my old mobile Samsung C3050. The media I wanted to download was just an audio mp3 file that I usually download easily.

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

The error appears to be "Unknown content type." So, as my only HTTP header Content-Type was "application/force-download", I try to comment this and try again. Then, it works. But now, I am currently asking what this Content-Type means and if it can be mandatory for others mobile. I tested without the Content-Type on the iPhone 4 and it works, but I'm not sure of this compatibility for all mobile.

错误似乎是“未知的内容类型”。因此,由于我唯一的 HTTP 标头 Content-Type 是“application/force-download”,我尝试对此发表评论并重试。然后,它起作用了。但是现在,我目前正在问这个 Content-Type 是什么意思,以及它是否可以强制用于其他移动设备。我在 iPhone 4 上没有 Content-Type 的情况下进行了测试,它可以工作,但我不确定这种兼容性是否适用于所有移动设备。

Can someone explain me how that Content-Type works, why this isn't a standard MIME or everything else that can help me to be sure this is an optionnal Content-Type for every download, whatever the file, the browser or the device I am downloading on?

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

Thanks everyone.

谢谢大家。

Here is my PHP headers sent:

这是我发送的 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);
?>

EDIT : I just tried with a Sony Xperia, and the download wasn't successful: I only see the "html-encoded" bytes of my file I want to download. How can I know what content-type I have to use if application/octet-stream or application/force-download doesn't work?

编辑:我刚刚尝试使用 Sony Xperia,但下载不成功:我只看到我想要下载的文件的“html 编码”字节。如果 application/octet-stream 或 application/force-download 不起作用,我怎么知道我必须使用什么内容类型?

回答by Quentin

Content-Type: application/force-downloadmeans "I, the web server, am going to lie to you (the browser) about what this file is so that you will not treat it as a PDF/Word Document/MP3/whatever and prompt the user to save the mysterious file to disk instead". It is a dirty hack that breaks horribly when the client doesn't do "save to disk".

Content-Type: application/force-download意思是“我,网络服务器,将向你(浏览器)说谎这个文件是什么,这样你就不会把它当作 PDF/Word 文档/MP3/任何东西,并提示用户将神秘文件保存到磁盘”。这是一个肮脏的黑客,当客户端不执行“保存到磁盘”时会严重破坏。

Use the correct mime type for whatever media you are using (e.g. audio/mpegfor mp3).

为您使用的任何媒体(例如audio/mpegmp3)使用正确的 MIME 类型。

Use the Content-Disposition: attachment; etc etcheader if you want to encourage the client to download it instead of following the default behaviour.

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

回答by Suresh kumar

To download a file please use the following code ... Store the File name with location in $file variable. It supports all mime type

要下载文件,请使用以下代码...将文件名和位置存储在 $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);

To know about Mime types please refer to this link: http://php.net/manual/en/function.mime-content-type.php

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

回答by Jon Grant

application/force-downloadis not a standard MIME type. It's a hack supported by some browsers, added fairly recently.

application/force-download不是标准的 MIME 类型。这是最近添加的某些浏览器支持的 hack。

Your question doesn't really make any sense. It's like asking why Internet Explorer 4 doesn't support the latest CSS 3 functionality.

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