简体   繁体   English

如何用php设置文件的mime类型?

[英]How to set a mime type on a file with php?

I am writing a Unit Test for a class and need to generate a few files with different mime-types. 我正在为一个类编写单元测试,需要生成一些具有不同mime类型的文件。

I know how to set the mime-type when sending a file to a remote user (ala header() ), but how to do it when using fwrite() on the local server? 我知道如何在向远程用户(ala header() )发送文件时设置mime类型,但是如何在本地服务器上使用fwrite()时这样做?

For a file generated in this method is the mime-type derived directly from the file extension? 对于在此方法中生成的文件是直接从文件扩展名派生的mime类型?

I'm using PHP 5.3.x on Ubuntu 12.04 我在Ubuntu 12.04上使用PHP 5.3.x.

Files don't have mime types. 文件没有mime类型。 They are identified by their file extensions. 它们由文件扩展名标识。 The mime types are usually set by file extensions in /etc/mime.types . mime类型通常由/etc/mime.types的文件扩展名设置。 All the mime type does is tell the client what program to use to open the file. 所有mime类型都告诉客户端用于打开文件的程序。

mime-type usually makes sense for web browsers and email clients only NOT for your physical files. mime-type通常只适用于Web浏览器和电子邮件客户端,而不适用于您的物理文件。 Just create your files with correct extensions and Ubuntu File Manager will interpret it accordingly. 只需使用正确的扩展名创建文件,Ubuntu文件管理器就会相应地解释它。

The mime-type belongs to the HTTP request your server or browser does. mime-type属于您的服务器或浏览器所执行的HTTP请求。 If you're running Apache, your can set default mime-types for your media files in .htaccess file. 如果您正在运行Apache,则可以在.htaccess文件中为媒体文件设置默认的mime-types。 Then you can try to mock the request return your files with CURL or fsockopen and look in the header if the mime-type was correctly... just in case you want make sure in your unit test, that mime-types work correctly. 然后你可以尝试模拟请求返回你的文件与CURL或fsockopen并查看标题,如果mime类型是正确的...以防万一你要确保在你的单元测试中,mime类型正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM