lundi 27 juin 2016

How to upload multiple images with php and sql

I can upload one image at a time with my code, however it I have added multiple="multiple" and it lets me highlight more than one image to upload but only one image gets uploaded to the sql database.

Here is my form below -

<form action="upload_image.php" method="post" enctype="multipart/form-data">
    <p>Select files:<br /><input type="file" name="image[]" multiple="multiple"/></p>
    <p>
    Choose an album<br />
    <select name="album_id">
        <?php
        foreach ($albums as $album) {
                echo '<option value="', $album['id'], '">', $album['name'], '</option>';

        }
        ?>

    </select>
</p>
<p><input type="submit" value="Upload" /></p>
</form>

and my PHP side looks like this -

<?php
if (isset($_FILES['image'], $_POST['album_id'])) {
$image_name = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_temp = $_FILES['image']['tmp_name'];
$image_dimension = $_FILES['image']['dimension'];


$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$image_ext = strtolower(end(explode('.', $image_name)));
$album_id = $_POST['album_id'];

$errors = array();

    if (empty($image_name) || empty($album_id)) {
        $errors[] = 'Something is missing';
    } else {

        if (in_array($image_ext, $allowed_ext) === false) {
            $errors[] = 'File type not allowed';
        }

        if($image_size > 10485760){
            $errors[] = 'Maximum file size is 10MB'; 
        }

        if (album_check($album_id) === false) {
            $errors[] = 'Couldn't upload to that album';

        }
    }

if (!empty($errors)) {
    foreach ($errors as $error) {
        echo $error, '<br />';
    }

} else {
    upload_image($image_temp, $image_ext, $album_id);
    header('Location: view_album.php?album_id='.$album_id);
    exit();
    }

}

Any help on this would be amazing!

Regards Aaron

Aucun commentaire:

Enregistrer un commentaire