Foot Blogger Foot Blogger - 7 months ago 19
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

multiple
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
$photo_path
folder, but doesn't add the selected 5 images to the
$zip_path
folder.

Any ideas where I'm going wrong?

Update:

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

move_uploaded_file($_FILES["gallery"]["tmp_name"],"$zip_path");

}

Answer

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");
    }
}
Comments