multiple image upload using php

Upload multiple images using PHP and MySQL.

Welcome back to shortlearner.com . Today we will see how to upload more than 5 images in database with the help of php and mysql.

multiple image upload using php

so before start this tutorial let me clear the over all scenario of this post.we will create three PHP page here ,
in the very first step we establish our database connection .

in the second step we design a responsive form for uploading images and in the very last step
we will start to write code for uploading images in a specific folder and stores image name inside the database table.

upload multiple images using php

connection.php

In this first step we write a code which are use for establishing a database connection.
we have our test_db database.
and the username and password are same root.

<?php 
$db_con= mysqli_connect("localhost","root","root","test_db");
?>

index.php

in this page we are design a html form which will use to take images from local system .

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
    <table width="100%">
        <tr>
            <td>Select Photo (one or multiple):</td>
            <td><input type="file" name="files[]" multiple/></td>
        </tr>
        <tr>
            <td colspan="2" align="center">Note: Supported image format: .jpeg, .jpg, .png, .gif</td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="Create Gallery" id="selectedButton"/></td>
        </tr>
    </table>
</form>
</body>
</html>

In the upload.php page we will fetch images from local system and check the extensions of all images and store inside the storage folder.

upload.php

<?php extract($_POST);
$error=array();
$extension=array("jpeg","jpg","png","gif");
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name) {
    $file_name=$_FILES["files"]["name"][$key];
    $file_tmp=$_FILES["files"]["tmp_name"][$key];
    $ext=pathinfo($file_name,PATHINFO_EXTENSION);

    if(in_array($ext,$extension)) {
        if(!file_exists("storage/".$txtGalleryName."/".$file_name)) {
            move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$file_name);
        }
        else {
            $filename=basename($file_name,$ext);
            $newFileName=$filename.time().".".$ext;
            move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$newFileName);
        }
    }
    else {
        array_push($error,"$file_name, ");
    }
} 
?>

Read More: Date calculatio