troubleshooting Question

Validate Upload Script

Avatar of PeterErhard
PeterErhard asked on
PHP
6 Comments2 Solutions296 ViewsLast Modified:
I'll be allowing users to upload certain files on my website and have put together the below script.

Can anyone see any problems with it? Any suggested improvements?

<?php

      $FileUploadSuccess = 0;

      $uploaddir = 'files/';
      $uploadfile = $uploaddir . $_FILES['img1']['name'];

      $filetypes = array("image/jpg","image/gif","image/jpeg","image/pjpeg");
      $imageinfo = getimagesize($_FILES['img1']['tmp_name']);

      //check file size is less than 2MB (below is bytes)
      if ($_FILES['img1']['size'] <=  2097152)
      {
            // upload the file only if the file type is of one listed above.
            if(in_array(strtolower($_FILES['img1']['type']),$filetypes))
            {
                  // rename the file.
                  $path_parts = pathinfo($uploadfile);
                  $uploadfile = $uploaddir.(substr($_FILES['img1']['name'],0,(strlen($_FILES['img1']['name'])-(strlen($path_parts['extension'])+1))).date("YmdHis").".".$path_parts['extension']);

                  if (move_uploaded_file($_FILES['img1']['tmp_name'], $uploadfile))
                  {
                        $FileUploadSuccess = 1;
                  }
            }
      }

      if ($FileUploadSuccess == 0)
      {
            echo "File Upload Failed for some reason - display error message with likely reasons";
      }
      else
      {
            echo "File Upload Successful - display happy message";
      }

?>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros