File Upload Via PHP

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. 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

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. The source code follows:


Specific MIME Type

The purpose of this code, as I mentioned before, was to be part of the photoblog software and as such, it wouldn’t make much sense to upload anything but graphic files (JPEG/GIF/PNG). Also, once the graphic file is uploaded to the server, more often than not it needs to be resized (thumbnails). I will probably include those parts in a subsequent post.