How to set a mime type on a file with php?

Patrick picture Patrick · Feb 5, 2013 · Viewed 8.7k times · Source

I am writing a Unit Test for a class and need to generate a few 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 it when using fwrite() on the local server?

For a file generated in this method is the mime-type derived directly from the file extension?

I'm using PHP 5.3.x on Ubuntu 12.04

Answer

Rob picture Rob · Feb 5, 2013

Files don't have mime types. They are identified by their file extensions. The mime types are usually set by file extensions in /etc/mime.types. All the mime type does is tell the client what program to use to open the file.