关于 HTTP Header Content-Type: application/force-download 不生效

今天在了解更多关于 Accept Header 信息的时候,看到 segmentfault 中的这个回答提到了我没见过的一个 MIME type,然后大概搜索了一下资料了解了一下。

在 stackoverflow 中有一个问题:Utility of HTTP header “Content-Type: application/force-download” for mobile? 回复如下:

@Quentin answered May 16 ‘12 at 10:34, edited Jan 29 ‘14 at 14:02

Content-Type: application/force-download means “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”.

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

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

@Suresh kumar answered May 16 ‘12 at 9:42

@catfood edited Nov 5 ‘13 at 14:09

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

1
2
3
4
5
6
7
8
9
10
11
12
$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

@Jon Grant answered May 16 ‘12 at 10:36

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

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

简单来说就是 application/force-download 这不是一个标准的 MIME type,只是部分浏览器的实现,所以如果需要下载文件还是使用 Content-Disposition 吧!参考上述回复中给出的相关内容。


原问题如下:

@niconoe asked May 16 ‘12 at 9:34, edited May 16 ‘12 at 10:31

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.

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.

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?

Thanks everyone.

Here is my PHP headers sent:

1
2
3
4
5
6
7
<?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?