demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

Enabling User Uploads: Checking The File

Accepting user uploads to our site requires both validation and security: validation to check that the file is correct (the right size and format) and security to ensure that nothing nefarious is being uploaded. So far in we’ve made a form and a basic PHP page that transfers the file; what we want now is to make that transfer conditional on the file passing several tests.

Turning back to the PHP for transferring the file, we’ll add two lines. These additions will not prevent the file from being uploaded, but they will give us a hint as to what we should inspect to see if we should do so:

  1. <?php
  2. $upload_tmp_dir = “/tmp”;
  3. $imageinfo = getimagesize($_FILES['upload'] ['tmp_name']);
  4. print_r($imageinfo);
  5. $uploadedfile = “images/{$_FILES['upload']['name']}”;
  6. if (move_uploaded_file ($_FILES['upload'] ['tmp_name'], $uploadedfile )) {
  7. echo "File successfully uploaded";
  8. } else {
  9. echo "Failure to upload"; ?>

Run the form you previously made, and take a look at what the receiving page now prints out.

getimagesize is actually a little bit of a normer; while the function does give us information about the image's dimensions, it also looks at the MIME type and other details. It is this information, along with the physical size of the file in bytes, that we want to check. Let's say you only wanted to accept JPEG and PNG images. To cover the bases, you could wrap the move_uploaded_file function with an if statement:

  1. if ($imageinfo['mime'] == ("image/png" || "image/jpg" ||
  2. "image/jpeg" || "image/pjpg"))
  3. {
  4. $uploadedfile = “images/{$_FILES['upload']['name']}”;
  5. if (move_uploaded_file ($_FILES['upload'] ['tmp_name'], $uploadedfile )) {
  6. echo "<p>File successfully uploaded>/p<";
  7. } else {
  8. echo "<p>Failure to upload>/p<";
  9. }
  10. } else {
  11. echo "<p>Your file was not accepted.>/p<";
  12. }

If you wanted to add a check of the file size, the if condition could be altered to include:

  1. if ($imageinfo['mime'] == ("image/png" || "image/jpg" ||
  2. "image/jpeg" || "image/pjpg")
  3. && $imageinfo['size'] > 61440)

(Note that this file size limit is the same as the MAX_FILE_SIZE we set in our original form.)

There are many more tests that we could do, but these two conditions would be enough to ensure that your site is receiving the kind of file that you want to accept while keeping basic security protocols in place.

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.