How to set mime type in file with php?

I am writing Unit Test for a class and you need to generate several files with different mime types.

I know how to set the mime type when sending a file to a remote user (ala header() ), but how to do this when using fwrite() on the local server?

For the file generated in this method, is the mime type obtained directly from the file extension?

I am using PHP 5.3.x on Ubuntu 12.04

+6
source share
3 answers

Files do not have mime types. They are determined by their file extensions. /etc/mime.types types are usually set by file extensions in /etc/mime.types . The entire mime type tells the client which program to use to open the file.

+6
source

mime-type usually makes sense for web browsers and email clients only for your physical files. Just create your files with the correct extensions, and Ubuntu File Manager will interpret it accordingly.

+3
source

The mime type belongs to the HTTP request of your server or browser. If you use Apache, you can set default mime types for your media files in a .htaccess file. Then you can try to make fun of the request, return your files using CURL or fsockopen and look in the header if the mime type was correct ... just in case you want to make sure in your unit test that the mime types work correctly.

+1
source

All Articles