Resolving file upload error 4

I am trying to upload multiple files to my server. If I try to upload a single file, it works fine. but if I try more than one, it will give me error code 4, although it will correctly print the name of all files. Nothing uploaded. I have the correct input type. Can anybody help me?

Choose Image:&nbsp;&nbsp;<input name="uploadedfile[]" type="file" multiple="true"/><br /><br /><br /> <input type="submit" value="Upload Image!" style="margin-left:100px;"/> 

Below is the code:

 $i=0; foreach($_FILES['uploadedfile']['name'] as $f) { $file['name'] = $_FILES['uploadedfile']['name'][$i]; $file['type'] = $_FILES['uploadedfile']['type'][$i]; $file['tmp_name'] = $_FILES['uploadedfile']['tmp_name'][$i]; $file['error'] = $_FILES['uploadedfile']['error'][$i]; $file['size'] = $_FILES['uploadedfile']['size'][$i]; if ($file["error"] > 0) { echo "Error Code: " . $file["error"]; } $target_path = "uploads/".basename($file["name"]); if(move_uploaded_file($file["tmp_name"], $target_path)) { echo basename($file['name'])."<br />"; echo basename($file['tmp_name'])."<br />"; echo $target_path; } else{ echo "There was an error uploading the file, please try again!"; } $i++; } 

and my HTML form

 <div id="album_slider"> <div style="text-align:center;margin:20px auto;font-size:27px;">Upload Image</div> <br style="clear:both;font-size:0;line-height:0;height:0;"/> <div style="width:700px;margin:auto;height:250px;text-align:left;"> <form enctype="multipart/form-data" action="uploader.php" method="POST" name="form"> Image Name:&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="image_name" id="image_name"/><br /><br /><br /> <input type="hidden" name="a_id" id="a_id" value="<?php echo $a_id; ?>"/> Choose Image:&nbsp;&nbsp;<input name="uploadedfile[]" type="file" multiple="true"/><br /><br /><br /> <input type="submit" value="Upload Image!" style="margin-left:100px;"/> </form> </div> <br style="clear:both;font-size:0;line-height:0;height:1px;"/> </div> 
+7
source share
3 answers

Try:

  foreach ($ _FILES ["uploadedfile"] ["error"] as $ key => $ error) {
    if ($ error == UPLOAD_ERR_OK) {
        echo "$ error_codes [$ error]";
        move_uploaded_file (
          $ _FILES ["uploadedfile"] ["tmp_name"] [$ key],
          $ target_path
        ) or die ("Problems with upload");
    }
 }
+6
source

Well, this will happen if you want to download MULTIPLE files, but your code is only for processing one separate file.

Go ahead and read how to handle multiple files here:
http://php.net/manual/en/features.file-upload.multiple.php

+3
source

Use status check as below code

 if(isset($_FILES["qImage"]) && !empty($_FILES["qImage"]["name"])){ $imgSubQuestion = $_FILES["qImage"]; } if(isset($_FILES["sImage"]) && !empty($_FILES["sImage"]["name"])){ $imgSolution = $_FILES["sImage"]; } 
0
source

All Articles