0

I'm trying to create part of a form that uploads 5 images. At the moment I'm just trying to fix the validation of the images. Up to 5 images can be uploaded, the form contains 5 input fields for these (I know you can upload multiple files in one input but the design is designed like this, not my choice).

So firstly I check to see if the image checkbox has been checked (this runs fine) and then basically I try and check to see if there is an image uploaded, and then for each one with an image I try and run some basic validation (file size & type).

$imagesArray = array("order-desc-img", "order-desc-img-1","order-desc-img-2", 
               "order-desc-img-3", "order-desc-img-4"); //names of the file inputs

   if (isset($_POST['images'])){ //check checkbox
            $imagesQuantity = 1; //update variable to show it has been checked

        foreach ($imagesArray as $image) { //loop through each item in array
                $image_target_dir = "../uploads/images/";
                $image_name = basename($_FILES[$image]["name"]);
                $image_target_file = $image_target_dir . date('m-d-Y_hia') . '_' . $image_name;

            if ($image_name != ''){ //if no name from file input
                $imageFileType = pathinfo($image_target_file,PATHINFO_EXTENSION);

                if ($_FILES[$image]["size"] > 5000000000) {
                    $errors++;
                };//place holder file size check

                if($imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType != "png") {
                    $errors++;
                };//placeholder file type check
             }
        }

};

However if I only try to upload one image the validation fails. If I try to upload all 5 I'm not sure what happens, it just stops on the validation page and never reaches the header for failing or successing. I'm not so great with debugging so I'm not sure how to figure out what I'm doing wrong. Am I going about it the wrong way?

Any help would be appreciated.

user1375823
  • 95
  • 1
  • 12
  • I think you should check `$_FILES[$image]["tmp_name"]` to validate a file before move it – Razorphyn Dec 05 '14 at 19:57
  • Yes I know, I'm not moving it yet. Like I said above just trying to fix what I have, which is the code above. – user1375823 Dec 05 '14 at 20:01
  • and this to check if it's an image: [check if image](http://stackoverflow.com/questions/6755192/uploaded-file-type-check-by-php), also you can try to use [file_put_contents](http://php.net/manual/it/function.file-put-contents.php) to see where it stops (just create multiple file) and in case print some results, also check the error_log if it exists – Razorphyn Dec 05 '14 at 20:04
  • Use `file_put_contents('postarray', print_r($_POST,true));` to print the `$_POST` array and check the array value – Razorphyn Dec 05 '14 at 20:09

0 Answers0