Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubleshooting file upload error code 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 gives me an error code 4, even though it does print the name of all the files correctly. Nothing is uploaded. I have the input type set correctly. Can someone 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>
like image 840
pat Avatar asked Dec 06 '11 01:12

pat


1 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");
   }
}
like image 122
Sudhir Bastakoti Avatar answered Oct 19 '22 05:10

Sudhir Bastakoti