user1678514
user1678514

Reputation: 21

PHP Multiple File Upload Return Code Array

I'm trying to upload multiple files but I'm getting "Error: Array". I've posted the form and php below.

if(isset($_POST['upload'])){
        $count = 0;
        foreach($_FILES["file"]["name"] as $filename){
            $count = $count + 1;
            $tmp = $_FILES["file"]["tmp_name"][$count];
            $size = $_FILES["file"]["size"];
            $error = $_FILES["file"]["error"];
            $type = $_FILES["file"]["type"];
        }
        if($error > 0){
            $stat = "Error: $error<br />";
        } else {
            move_uploaded_file($tmp,"uploads/$filename");
        }   
    }

<form method="POST" enctype="multipart/form-data">
    <input type="file" name="file[]" id="file" multiple/><br />
    <input type="submit" name="upload" value="Upload">
</form>

Upvotes: 0

Views: 593

Answers (1)

David
David

Reputation: 2065

The problem is $_FILES is a multi-dimensional array. If you have 2 file fields that are submitted, it would have two values under each index (e.g. ['tmp_name'][0] and ['tmp_name'][1]).

Therefore the $_FILES['file']['error'] contains an array of errors for the file fields.

You can either loop through these to view them, for example:

foreach($error AS $err) {
  echo $err . '<br />';
}

Or print_r($error); to print out the array (Only wise for development, not production).

Hope that helps.

Upvotes: 1

Related Questions