Handling File Uploads using PHP

On a recent work project, I had to create a little system that allows users to upload files from their home machines to a Web server. It was a much more difficult problem than I thought it’d be.

It took me a few days to figure out the client side — a Web page that contains code to allow users to select files and send them. Once I had that part figured out, I turned my attention to the server side — fetching the files that were sent and then saving them.

In principle, any server-side technology can fetch uploaded files. But for my particular scenario, the two primary candidate technologies were 1.) a simple PHP script, 2.) a simple ASP.NET ASHX script.

I got a basic PHP version (no error-checking) working, after futzing around with getting PHP installed and working with IIS (I don’t use Apache very often).

The PHP code is:


 if (isset($_FILES['myFile'])) {
   echo $_FILES['myFile']['tmp_name'];
   echo "\n";
   echo $_FILES['myFile']['name'];
     "C:/Data/Junk/Uploads/" . $_FILES['myFile']['name']);


The client-side code sets a key named ‘myFile’ with value of the uploaded file. The server-side looks for that key and automatically saves the uploaded file with a temp name into a temp directory, then copies the temp file into a directory Uploads on the server.

The basic idea is simple, but there are tons of details.


This entry was posted in Miscellaneous. Bookmark the permalink.