Paul
Paul

Reputation: 3368

Looped file array not uploading all files

I am attempting to upload anywhere from 1 - x amount of files, based on what the user uploads. Recently, I created a for loop to go through the files. What I am seeing is only the first file uploads.

Does anyone see why only the first file would be uploading?

class fileUpload
{

    public function __construct()
    {}
    public function upload() {

        $file_count = count($_FILES['uploadedFile']['name']);
        //$file_count = count($_FILES($file_post['name']));

        for ($i = 0; $i<$file_count; $i++) {
//          echo $file['uploadedFile']['name'][$index] . "\n";

            $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["uploadedFile"]["name"][$i]);
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));        

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                return 0;
    // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["uploadedFile"]["tmp_name"][$i], $target_file)) {
                    return basename($_FILES["uploadedFile"]["name"][$i]);
                } else {
                    return 0;
                }
            }
        }

    }
}

Form input:

<input type="file" name="uploadedFile[]" class="inputfile" id="uploadedFileTest" data-multiple-caption="{count} files selected" multiple>

Upvotes: 0

Views: 69

Answers (1)

Soufiane Lamnizeh
Soufiane Lamnizeh

Reputation: 434

That's because you use the return, the return will get you out the function and you will get only the first item uploaded.

A simple fix would be:

if (!move_uploaded_file($_FILES["uploadedFile"]["tmp_name"][$i], $target_file)) {
    return 0;
}

//update 2 suggestion for a solution

public function upload() {

        $file_count = count($_FILES['uploadedFile']['name']);
        //$file_count = count($_FILES($file_post['name']));
            // add a array to save the basename on each loop
            $results = [];
        for ($i = 0; $i<$file_count; $i++) {
//          echo $file['uploadedFile']['name'][$index] . "\n";

            $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["uploadedFile"]["name"][$i]);
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));        

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                return 0;
    // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["uploadedFile"]["tmp_name"][$i], $target_file)) {
                    $results[] = basename($_FILES["uploadedFile"]["name"][$i]);
                } else {
                    return 0;
                }
            }
        }
        //return all basename in one shot
        return $results;

    }

Upvotes: 3

Related Questions