AbdeAMNR
AbdeAMNR

Reputation: 145

upload multiple files with move_uploaded_file() function by using foreach loop

I'm trying to upload multiple files with for each loop. what should I use in the first parameter of move_uploaded_file() function in this case

            foreach ($_FILES["prodImg"]["name"] as $pImage) {
                $nbr++;
                $col = 'image' . $nbr;
                $fileName = basename($pImage);
                $target_file = $target_dir . "" . $fileName;
                $rqt = "UPDATE prodimages SET $col=? WHERE prodId= ? ";
                $stmt = $con->prepare($rqt);
                $stmt->execute(array($fileName, $pID));
                move_uploaded_file($pImage, $target_file);
            }

Upvotes: 0

Views: 3020

Answers (1)

Halcyon
Halcyon

Reputation: 57703

As you can see in the docs you can use $_FILES["prodImg"]["tmp_name"][$i]:

foreach ($_FILES["prodImg"]["name"] as $i => $pImage) {
    move_uploaded_file($_FILES["prodImg"]["tmp_name"][$i], /*..*/);
}

You can always var_dump($_FILES); to see what it looks like.

Upvotes: 2

Related Questions