Ok, so I have an index.php file which has to process many different file types. how do I guess the filetype based on the REQUEST_URI
.
If I request http://site/image.jpg
, and all requests redirect through index.php, which looks like this
<?php include('/www/site'.$_SERVER['REQUEST_URI']); ?>
How would I make that work correctly?
Should I test based on the extension of the file requested, or is there a way to get the filetype?
For detecting MIME-types, use the aptly named "mimetype" command. It has a number of options for formatting the output, it even has an option for backward compatibility to "file". But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.
All MIME type information is stored in a database. The MIME database is located in the directory /usr/share/mime/ . The MIME database contains a large number of common MIME types, stored in the file /usr/share/mime/packages/freedesktop.
In the Connections pane, go to the site, application, or directory for which you want to add a MIME type. In the Home pane, double-click MIME Types. In the MIME Types pane, click Add... in the Actions pane. In the Add MIME Type dialog box, add the file name extension and MIME type, and then click OK.
If you are sure you're only ever working with images, you can check out the getimagesize() exif_imagetype() PHP function, which attempts to return the image mime-type.
If you don't mind external dependencies, you can also check out the excellent getID3 library which can determine the mime-type of many different file types.
Lastly, you can check out the mime_content_type() function - but it has been deprecated for the Fileinfo PECL extension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With