如何知道您强制用户下载的文件的文件类型?
我试图强制用户下载文件。为此,我的脚本是:
$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 中收到的图像的内容类型。 如何实现这一点
I am trying to force the user to download a file. For that my script is:
$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);
The files I am going to upload my not be .zip all the time so I want to know the content type of the image I am going to receive in $file.
How to accomplish this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我总是把
application/octet-stream
(或类似的东西)放在浏览器中,因为浏览器无法尝试以任何方式显示它,它总是会强制用户保存/运行,然后文件类型通常由扩展推断。I always put
application/octet-stream
(or somwething like that) as the browser can't then try and display it in any way, it will always force the user to Save / Run, then the file type is often inferred by the extension.对于图像,最可靠的是 getimagesize()。对于任何其他类型, Fileinfo 函数是正确的。
不过,我同意 ck 的说法:强制下载时,octet-stream 是防止在浏览器或相应客户端应用程序中打开内容的最佳选择。
For images, the most reliable is getimagesize(). For any other type, the Fileinfo functions are the right thing.
I second what ck says, though: When forcing a download,
octet-stream
is the best choice to prevent things from opening in the browser or the respective client application.嘿,在下面的链接中,您可以看到所需的 Content-Type 值:
互联网媒体类型< /a>
这里太多了,无法列出
拉迪斯拉夫
Hey, on the link below you can see the values of the Content-Type that you need:
Internet Media Type
There are just too many to list here
Ladislav
好吧,我自己做的。
我用过
Ok, I did it myself.
I used
您需要将mime-type发送到那里。您可以通过使用
mime_content_type
函数在 PHP 中获取此信息。还有 Fileinfo 扩展,它提供了一种获取大量文件的方法信息,包括 mime 类型。You need to send the mime-type there. You can get this in PHP by using the
mime_content_type
function. There's also the Fileinfo extension which provides a way of getting lots of file information, including the mime type.