« Back to blog

Easy HTML5 Multi Image Uploader

Here is a simple tutorial using HTML5 for a Mutli File Uploader. I have also included the possibility to insert the multiple selected files into a database table. For this tutorial I will be using images as my upload files.

We will be creating the following:
- index.php
- upload.php
- uploads folder, remember to set the right permissions on your hosting service if U plan to use this.
- a database named "multi_file_uploader" with a table named "images" and a field named "image". If U don't want to insert the files into a database U can just skip this part.


Start by making a simple form layout, and don't forget the enctype='multipart/form-data'. Keep in mind that with a multiple file uploader you have to change a little bit of the code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transtitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transtitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>HTML 5 | Multi File Uploader</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <form action='upload.php' method='post' enctype='multipart/form-data'>
            <input type='file' name='images[]' multiple='multiple' />
            <input type='submit' value='Upload' />
        </form>
    </body>
</html>


As you can see above we have a few differences.

A previous file upload input field would look like :
input type='file' name='image'

With HTML5 this would be :
input type='file' name='images[]' multiple='multiple'


With HTML5 we create an array from our uploaded images. The multiple='multiple' code makes it possible to really select mutliple files at once. Without this it would obviously not select multiple images.

That's it for the upload form.

Now we go on to the upload.php since that is the action the form will perform whenever the submit button is clicked.

Below is the php code:

<?php

    // Skip these 2 lines if U do not plan on inserting your images into a database.

    mysql_connect("localhost", "root", "") or die(mysql_error());

    mysql_select_db("multi_file_uploader") or die(mysql_error());



    if(isset($_FILES['images'])){



        //Here it takes all the images from the array, selects them 1 by 1 with the foreach loop,
and uploads it to the upload location, in this case the uploads folder we created earlier.         foreach($_FILES['images']['tmp_name'] as $key => $temp){                         $image = $_FILES['images']['name'][$key]; // get the image name             $location = "uploads/". $image; // location where ur image will be stored at             move_uploaded_file($temp, $location); // move image from temporary location to upload location                         // Skip the following line if U do not plan on inserting your images into a database.             mysql_query("INSERT INTO `images` (image) VALUES ('" .$image. "')");         }         // Return back to index.php         header("Location: index.php");     } ?>


And that was it. Your multiple selected images will now be in you uploads folder.

If U have any questions or comments feel free to drop a line below.
venta de viagra
tpnblhgx, <a href="http://burnsqh.com">cialis</a>, [url="http://burnsqh.com"]cialis[/url], http://burnsqh.com cialis, wsleceam, <a href="http://gsiestates.com">viagra online</a>, [url="http://gsiestates.com"]viagra online[/url], http://gsiestates.com viagra online, rtgekbzd, <a href="http://confectionscarcajou.com">prix levitra</a>, [url="http://confectionscarcajou.com"]prix levitra[/url], http://confectionscarcajou.com prix levitra, ebqnpjkc,
viagra online
aerppfhf, <a href="http://gsiestates.com">viagra</a>, [url="http://gsiestates.com"]viagra[/url], http://gsiestates.com viagra, eagffbmf, <a href="http://burnsqh.com">cialis prix en pharmacie</a>, [url="http://burnsqh.com"]cialis prix en pharmacie[/url], http://burnsqh.com cialis prix en pharmacie, idsoldrw, <a href="http://confectionscarcajou.com">achat levitra france</a>, [url="http://confectionscarcajou.com"]achat levitra france[/url], http://confectionscarcajou.com achat levitra france, xvwsogyx,
fevaeabj
DaczXF <a href="http://icswmbsbfsky.com/">icswmbsbfsky</a>, [url=http://zhlxyprhcqbh.com/]zhlxyprhcqbh[/url], [link=http://oybiiuvkhrnd.com/]oybiiuvkhrnd[/link], http://fdyvyakcryje.com/
wygzvkfw
c436P8 <a href="http://ytxpvvbprbqm.com/">ytxpvvbprbqm</a>, [url=http://cthxmprzfwnc.com/]cthxmprzfwnc[/url], [link=http://ffxqbkybxkac.com/]ffxqbkybxkac[/link], http://ewggcwweobuc.com/