Foot Blogger Foot Blogger - 2 years ago 89
PHP Question

Uploading Multiple Images With An Input File Field

I'm building a simple website where I want to upload images into a folder so I'm doing this by enabling

in the input box.

Here is my HTML:

<input type="file" name="gallery[]" multiple />

And here is the PHP:

$id = time();
$year = date("Y");
$photo_path = "photos/$year/";
$zip_path = "sets/$year/";

$photo = $_FILES["photo"]["name"];
$gallery = $_FILES["gallery"]["name"];

move_uploaded_file($_FILES["photo"]["tmp_name"],"$photo_path" . $id . ".jpg"); // This Adds Photo To $photo_path
move_uploaded_file($_FILES["gallery"]["tmp_name"],"$zip_path"); // This Doesn't Add 5 Files To $zip_path

This adds the photo into the
folder, but doesn't add the selected 5 images to the

Any ideas where I'm going wrong?


foreach ($_FILES["gallery"] as $file) {



Answer Source

You need to handle multiple files in a loop. Here is an example directly from the manual.

$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download