multi upload php

0

How do I get this script to receive multiple images, at the moment it only takes one at a time.

<?php
    include 'connection.php';
    $album_id = $_GET['album_id'];
    if ($_FILES['photo']['name'] != null) 
    {
        move_uploaded_file($_FILES['photo']['tmp_name'], "images/". $_FILES['photo']['name']);
        $photo_link = "images/". $_FILES['photo']['name'];     
        $upload_photo = $mysqli->query("INSERT INTO gallery_photos (album_id, photo_link) VALUES ($album_id, '$photo_link')");

        if ($upload_photo) 
        {
            header("Location: view-album.php?album_id=$album_id&amp;upload_action=success");
        } 
        else 
        {
            echo $mysqli->error;
        }

    } 
    else 
    {
        header("Location: index.php");
    }
    ?>;

    arquivo de upload_photo.php


<form method="post" action="upload_photo.php?album_id=<?php echo $album_id ?>" enctype="multipart/form-data">
<label>Add photo to this album:</label><br>
<input type="file" name="photo" /> 

<input type="submit" name="upload_photo" value="Upload" />
</form>
<?php
if (isset($_GET['upload_action'])) {
if ($_GET['upload_action'] == "success") { ?>
<br><br>Photo successfully added to this album<br><br>
<?php }
}
?>
    
asked by anonymous 19.07.2017 / 02:12

2 answers

1

For this you have to go through the array (use the html5 multiple)

<input type="file" name="imagem[]" multiple>

foreach($_FILES["imagem"]["tmp_name"] as $key=>$tmp_name){
                $file_name = $_FILES["imagem"]["name"][$key];
                $file_tmp = $_FILES["imagem"]["tmp_name"][$key];
                $ext = pathinfo($file_name,PATHINFO_EXTENSION);
                if(in_array($ext,$extension)){
                    if(!file_exists("galeria/".$txtGalleryName."/".$file_name))
                    {
                        move_uploaded_file($file_tmp=$_FILES["imagem"]["tmp_name"][$key],"galeria/".$txtGalleryName."/".$file_name);
                    }
                    else
                    {
                        $filename = basename($file_name,$ext);
                        $newFileName = $filename.time().".".$ext;
                        move_uploaded_file($file_tmp=$_FILES["imagem"]["tmp_name"][$key],"galeria/".$txtGalleryName."/".$newFileName);
                    }
                }
                else
                {
                    array_push($error,"$file_name, ");
                }
            }
    
19.07.2017 / 02:17
0

In your last question forgetting to put the% example_configure

<form action="upload.php" method="post" enctype="multipart/form-data">
    <label>Add New Album</label>
    <input type="file" name="file[]" multiple>
    <button type="submit" name="submit_album">Enviar</button>
</form>
    
19.07.2017 / 14:39