Starting Vocational Training From 1-May-2024 Get Detail



Upload Multiple Images and Store in Database using PHP and MySQL

By : Admin     |     Website,Programing Knowledge     |     2nd November 2018

Step 1) Add HTML:


<form role="form" name="" method="post" enctype="multipart/form-data" action="image-upload-do.php">
    <div class="form-group">
        <label>Gallery Image</label>
        <input required name="image[]" multiple type="file" class="form-control">
    </div>
    <button type="submit" class="btn btn-success btn-sm">Submit </button>
</form>


Step 2) Create Database :


create database image_upload;


Step 3) Create Table :


create table image(id int primary key AUTO_INCREMENT,img_ext varchar(6));


Step 4) Upload Multiple Files in PHP (image-upload-do.php)


$db = new PDO(mysql:host=localhost;dbname=image_upload;charset=utf8mb4, root, ); //database connection
foreach($_FILES[image][tmp_name] as $key => $tmp_name) {

        $img_ext = pathinfo($file_name, PATHINFO_EXTENSION);

        $q = $db-> query("insert into image(img_ext)  values($img_ext)") or die("error");
            $lastID = $db-> lastInsertId(); 
          $imageNewName = $lastID.".".$img_ext; move_uploaded_file($file_tmp, "image/".$imageNewName);
        }