1

我试图强制用户下载文件。为此,我的脚本是:

$file = "file\this.zip";        
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip"); //This is what I need
header("Content-Transfer-Encoding: binary");
readfile($file);

我要上传的文件不是 .zip,所以我想知道我将在 $file 中收到的图像的内容类型。如何做到这一点

4

5 回答 5

5

我总是放application/octet-stream(或类似的东西),因为浏览器无法尝试以任何方式显示它,它总是会强制用户保存/运行,然后文件类型通常由扩展名推断。

于 2010-05-25T07:56:50.270 回答
2

对于图像,最可靠的是getimagesize()。对于任何其他类型,Fileinfo函数都是正确的。

不过,我赞同 ck 所说的:强制下载时,octet-stream是防止在浏览器或相应的客户端应用程序中打开内容的最佳选择。

于 2010-05-25T07:58:23.243 回答
0

嘿,在下面的链接中,您可以看到所需的 Content-Type 的值:

互联网媒体类型

这里列出的太多了 拉迪斯拉夫

于 2010-05-25T07:57:08.473 回答
0

好的,我自己做的。

我用了

$type = filetype($file); //to know the type
于 2010-05-25T08:00:18.873 回答
0

您需要将mime-type发送到那里。您可以使用该mime_content_type函数在 PHP 中获取此信息。还有Fileinfo扩展,它提供了一种获取大量文件信息的方法,包括 mime 类型。

于 2010-05-25T08:11:29.157 回答