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

发布于 2024-09-03 08:08:52 字数 411 浏览 3 评论 0原文

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

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

盗琴音 2024-09-10 08:08:52

我总是把 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.

辞别 2024-09-10 08:08:52

对于图像,最可靠的是 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.

诠释孤独 2024-09-10 08:08:52

嘿,在下面的链接中,您可以看到所需的 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

对你再特殊 2024-09-10 08:08:52

好吧,我自己做的。

我用过

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

Ok, I did it myself.

I used

$type = filetype($file); //to know the type
初见 2024-09-10 08:08:52

您需要将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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文