multi upload php

Asked

Viewed 408 times

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 }
}
?>
  • Related: https://answall.com/questions/121377/upload-multiplas-photos-com-subtitles

2 answers

1

For this you need 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, ");
                }
            }

0

On his last question had forgotten to put the html for example

<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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.