Php 如何知道您强制用户下载的文件的文件类型?

Php 如何知道您强制用户下载的文件的文件类型?,php,file-handling,Php,File Handling,我试图强迫用户下载一个文件。为此,我的脚本是: $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

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

$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中收到的图像的内容类型。
如何做到这一点

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

嘿,在下面的链接上,您可以看到所需内容类型的值:

这里要列出的东西太多了
Ladislav

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

不过,我赞同ck的说法:当强制下载时,
八位字节流是防止在浏览器或相应的客户端应用程序中打开内容的最佳选择。

好的,我自己做的

我曾经

$type = filetype($file); //to know the type

您需要将mime类型发送到那里。您可以通过使用函数在PHP中实现这一点。还有一个扩展名,它提供了一种获取大量文件信息的方法,包括mime类型。

它只会为所有文件返回“file”。不是你想要的。是的,我刚注意到。我已经按照建议使用了application/octet流