File Uploads Via PHP : Specific MIME Types

Disclaimer

These instructions/steps worked for me in CentOS. It may very well work for you on Red Hat-like or other distributions. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, sgowtham.com, nor its author is responsible for any/all damage – intellectual or otherwise.


During my recent, still on-going, attempts to write a personally satisfying photoblog software/application, I happened to learn about File Uploads using PHP and in my previous post, I discussed the general procedure for doing so. There are numerous articles on the web and in the books, but what follows here is what worked for me – it is expected to serve as a Note2Self but if you find it useful, please feel free to do so.

A Form To Select The File

This portion is exactly the same as in previous post but for completeness sake, here it is. Let us suppose that the HTML file that displays this form is called NewFile.html and the PHP file that does the actual uploading work (and some more) is called Uploader.php.



Select A File



Uploader.php

Suppose that the end-user selected a file to upload and hit the Upload This File button. Now, the file, Uploader.php takes over and does a few things. But let us suppose that only image file types (JPEG, GIF, PNG) are to be allowed as uploadable types. Let us also suppose that the image file so uploaded needs to be thumbnailed and put in a folder called thumbnails (located under the uploads folder). The source code follows: