how upload multiple images using one form in php

how upload multiple images using one form in php?

Here am writing tutorial or php script code for inserting into database and uploading multiple images using single form or submit button you can use this code for uploading multiple images into the folder as well as database.
You just have need copy and past this code and use it..

PHP Code Here

<?php
//database connectivity
mysql_connect("localhost","root","")
mysql_select_db("images");

//php script for inserting into database and uploading multiple images using single form or submit button
if(isset($_POST['images']))
{
    $image1=$_FILES['image']['name'][0];
    $image1tmp=$_FILES['image']['tmp_name'][0];
    
    $image2=$_FILES['image']['name'][1];
    $image2tmp=$_FILES['image']['tmp_name'][1];
        
    $image3=$_FILES['image']['name'][2];
    $image3tmp=$_FILES['image']['tmp_name'][2];
        
    $image4=$_FILES['image']['name'][3];
    $image4tmp=$_FILES['image']['tmp_name'][3];

    move_uploaded_file($image1tmp, "word-images/$image1");
    move_uploaded_file($image2tmp, "word-images/$image2");
    move_uploaded_file($image3tmp, "word-images/$image3");
    move_uploaded_file($image4tmp, "word-images/$image4");
    
    mysql_query("INSERT INTO images (image1, image2, image3, image4) VALUES('$image1', '$image2', '$image3', '$image4')");
    echo "Your Images Insert Successfully !";
}

?>

When you write html script you have need one thing remember define name just a array type of your input type file field, because here we are uploading multiple files for more understanding you can use this html code and how we retrieve this file into php you can see php code.

Html Code Here

 <html>
    <head>
        <title>inserting into database and uploading multiple images using single form or submit button</title>
    </head>
<body>

<!-- Html Code Write here fror inserting into database and uploading multiple images using single form or submit button -->

<form method="post">
    <table border="1">
        <tr>
            <td width="166" height="34" align="left"  class="t11ft"><strong>Image 1</strong></td>
            <td colspan="2" align="left">
                <input type="file" name="image[]" id="image" />
            </td>
        </tr>
        <tr>
            <td width="166" height="34" align="left"  class="t11ft"><strong>Image 2</strong></td>
            <td colspan="2" align="left">
                <input type="file" name="image[]" id="image" />
            </td>
        </tr>
        <tr>
            <td width="166" height="34" align="left"  class="t11ft"><strong>Image 3</strong></td>
            <td colspan="2" align="left">
                <input type="file" name="image[]" id="image" />
            </td>
        </tr>
        <tr>
            <td width="166" height="34" align="left"  class="t11ft"><strong>Image 4</strong></td>
            <td colspan="2" align="left">
                <input type="file" name="image[]" id="image" />
            </td>
        </tr>
        <tr>
            <td width="166" height="34" align="left"  class="t11ft"><strong>Image 1</strong></td>
            <td colspan="2" align="left">
                <input type="submit" name="images" value="Insert Images" />
            </td>
        </tr>       
    </table>
</form>
</body>
</html>
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.