Upload Banyak Gambar Secara Bersamaan Menggunakan PHP MySQL

//lh4.googleusercontent.com/-eAXyxy2JJKA/AAAAAAAAAAI/AAAAAAAAGS4/OHgzqHzCvtQ/s512-c/photo.jpg
October 19, 2018




Selamat siang kali ini kami akan mencoba membahas bagaimana mengupload banyak gambar secara bersamaan menggunakan PHP MySQL, pada pembuatan aplikasi sederhana ini kami menggunakan PHP:PDO.

Aplikasi ini sangat bermanfaat ketika kalian igin mengupload gambar yang banyak secara bersamaan kedalam database, biasa aplikasi seperti ini banyak digunakan di perkantoran dan di institusi pendidikan yang kadang memerlukan banyak form untuk mengupload gambar yang banyak.

Ada beberapa yang harus dipersiapkan di antaranya adalah kalan harus terlebih dahulu membuat folder dengan nama vehicleimages folder ini adalah tempat penyimpanan file gambar yang di upload.

Berikutnya buat database dengan nama carrental, kemudian masukkan struktur database dibawah ini.

 

CREATE TABLE `tblvehicles` (
  `id` int(11) NOT NULL,
  `VehiclesTitle` varchar(150) DEFAULT NULL,
  `Vimage1` varchar(120) DEFAULT NULL,
  `Vimage2` varchar(120) DEFAULT NULL,
  `Vimage3` varchar(120) DEFAULT NULL,
  `Vimage4` varchar(120) DEFAULT NULL,
  `Vimage5` varchar(120) DEFAULT NULL,
  `RegDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `UpdationDate` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `tblvehicles` (`id`, `VehiclesTitle`, `Vimage1`, `Vimage2`, `Vimage3`, `Vimage4`, `Vimage5`, `RegDate`, `UpdationDate`) VALUES
(11, 'Toyota Avanza', 'about_services_faq_bg.jpg', 'banner-image.jpg', 'car_755x430.png', 'featured-img-1.jpg', 'featured-img-3.jpg', '2018-10-18 17:44:11', NULL),
(12, 'Toyota Rush', 'looking-used-car.png', 'listing_img3.jpg', 'knowledge_base_bg.jpg', 'featured-img-3.jpg', 'featured-img-1.jpg', '2018-10-18 17:44:46', NULL);


ALTER TABLE `tblvehicles`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `tblvehicles`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
COMMIT;
 

Berikutnya buat file dengan nama config.php file ini berisi script untuk koneksi kedatabase.

 <?php 
// DB credentials.
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','carrental');
// Establish database connection.
try
{
 $dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
}
catch (PDOException $e)
{
 exit("Error: " . $e->getMessage());
}
?> 

Kemudian buat file dengan nama index.php form ini berisi script sebagai form upload gambar.



 <?php
session_start();
error_reporting(0);
include('config.php');
if(strlen($_SESSION['alogin'])==0)
{ 
 header('location:index.php');
}
else{ 

 if(isset($_POST['submit']))
 {
  $vehicletitle=$_POST['vehicletitle'];
  $vimage1=$_FILES["img1"]["name"];
  $vimage2=$_FILES["img2"]["name"];
  $vimage3=$_FILES["img3"]["name"];
  $vimage4=$_FILES["img4"]["name"];
  $vimage5=$_FILES["img5"]["name"];

  move_uploaded_file($_FILES["img1"]["tmp_name"],"img/vehicleimages/".$_FILES["img1"]["name"]);
  move_uploaded_file($_FILES["img2"]["tmp_name"],"img/vehicleimages/".$_FILES["img2"]["name"]);
  move_uploaded_file($_FILES["img3"]["tmp_name"],"img/vehicleimages/".$_FILES["img3"]["name"]);
  move_uploaded_file($_FILES["img4"]["tmp_name"],"img/vehicleimages/".$_FILES["img4"]["name"]);
  move_uploaded_file($_FILES["img5"]["tmp_name"],"img/vehicleimages/".$_FILES["img5"]["name"]);

  $sql="INSERT INTO tblvehicles(VehiclesTitle,Vimage1,Vimage2,Vimage3,Vimage4,Vimage5) VALUES(:vehicletitle,:vimage1,:vimage2,:vimage3,:vimage4,:vimage5)";

  $query = $dbh->prepare($sql);
  $query->bindParam(':vehicletitle',$vehicletitle,PDO::PARAM_STR);
  $query->bindParam(':vimage1',$vimage1,PDO::PARAM_STR);
  $query->bindParam(':vimage2',$vimage2,PDO::PARAM_STR);
  $query->bindParam(':vimage3',$vimage3,PDO::PARAM_STR);
  $query->bindParam(':vimage4',$vimage4,PDO::PARAM_STR);
  $query->bindParam(':vimage5',$vimage5,PDO::PARAM_STR);
  $query->execute();
  $lastInsertId = $dbh->lastInsertId();
  if($lastInsertId)
  {
   $msg="Vehicle posted successfully";
  }
  else 
  {
   $error="Something went wrong. Please try again";
  }
 }

 ?>
 <!DOCTYPE html>
 <html>
 <head>
  <title>Add Gambar</title>
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  <script src="bootstrap/js/bootstrap.min.js"></script>
 </head>
 <body>
  <nav class="navbar navbar-default navbar-static-top">
   <div class="container-fluid">
    <div class="navbar-header">
     <a class="navbar-brand" href="https://www.tutorialswb.com/">TutorialsWB</a>
     <ul class="nav navbar-nav">
      <li class="active"><a href="index.php">Home</a></li>
     </ul>
    </div>
   </div>
  </nav>
  <div class="container">
   <div class="page-header">
    <h1 class="h2">Add Vehicle<a class="btn btn-success" href="view-vehicle.php" style="margin-left: 770px;"><span class="glyphicon glyphicon-user"></span>&nbsp; View Vehicles</a></h1><hr>
   </div>
   <?php if($error){?>
    <div class="alert alert-danger">
     <span class="glyphicon glyphicon-info-sign"></span>ERROR : <strong><?php echo htmlentities($error); ?></strong>
    </div>
   <?php } 
   else if($msg){?>
    <div class="alert alert-success">
     <span class="glyphicon glyphicon-info-sign"></span>SUCCESS : <strong><?php echo htmlentities($msg); ?></strong>
     </div><?php }?>
     

     <form method="post" enctype="multipart/form-data">
      
      <div class="form-group">
       <label for="gambar1">Enter Your Title Vehicle</label>
       <input type="text" class="form-control" name="vehicletitle" placeholder="Enter Your Title Vehicle" required="">
      </div>
      <div class="form-group">
       <label for="gambar1">upload your image 1</label>
       <input type="file" class="form-control" name="img1" required="">
      </div> 
      <div class="form-group">
       <label for="gambar1">upload your image 2</label>
       <input type="file" class="form-control" name="img2" required="">
      </div>
      <div class="form-group">
       <label for="gambar1">upload your image 3</label>
       <input type="file" class="form-control" name="img3" required="">
      </div>
      <div class="form-group">
       <label for="gambar1">upload your image 4</label>
       <input type="file" class="form-control" name="img4" required="">
      </div>
      <div class="form-group">
       <label for="gambar1">upload your image 5</label>
       <input type="file" class="form-control" name="img5" required="">
      </div>

      <div class="form-group">
       <button class="btn btn-default" type="reset">Cancel</button>&nbsp;
       <button class="btn btn-primary" name="submit" type="submit" >Save changes</button>

      </div>
     </form>
    </div>
   </body>
   </html>
   <?php } ?> 

Berikutnya buat file dengan nama view-vehicle.php file ini berfungsi untuk menampilkan data gambar dari database.



 <?php
session_start();
error_reporting(0);
include('config.php');
if(strlen($_SESSION['alogin'])==0)
{ 
 header('location:index.php');
}
else{

 if(isset($_REQUEST['del']))
 {
  $delid=intval($_GET['del']);
  $sql = "delete from tblvehicles  WHERE  id=:delid";
  $query = $dbh->prepare($sql);
  $query -> bindParam(':delid',$delid, PDO::PARAM_STR);
  $query -> execute();
  $msg="Vehicle  record deleted successfully";
 }


 ?>
 <!DOCTYPE html>
 <html>
 <head>
  <title>View Gambar</title>
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  <script src="bootstrap/js/bootstrap.min.js"></script>
 </head>
 <body>
  <nav class="navbar navbar-default navbar-static-top">
   <div class="container-fluid">
    <div class="navbar-header">
     <a class="navbar-brand" href="https://www.tutorialswb.com/">TutorialsWB</a>
     <ul class="nav navbar-nav">
      <li class="active"><a href="index.php">Home</a></li>
     </ul>
    </div>
   </div>
  </nav>
  <div class="container">
   <div class="page-header">
    <h1 class="h2">&nbsp; List Users<a class="btn btn-success" href="index.php" style="margin-left: 770px;"><span class="glyphicon glyphicon-user"></span>&nbsp; Back Home</a></h1><hr>
   </div>
   <?php if($error){?>
    <div class="alert alert-danger">
     <span class="glyphicon glyphicon-info-sign"></span>ERROR : <strong><?php echo htmlentities($error); ?></strong>
    </div>
   <?php } 
   else if($msg){?>
    <div class="alert alert-success">
     <span class="glyphicon glyphicon-info-sign"></span>SUCCESS : <strong><?php echo htmlentities($msg); ?></strong>
     </div><?php }?>
     
     <table class="table table-dark">
      <thead>
       <tr>
        <th scope="col">No</th>
        <th scope="col">Vehicless Title</th>
        <th scope="col">Deleted</th>
        <th scope="col">Edit</th>
       </tr>
      </thead>

      
      <?php $sql = "SELECT * from  tblvehicles ";
      $query = $dbh -> prepare($sql);
      $query->execute();
      $results=$query->fetchAll(PDO::FETCH_OBJ);
      $cnt=1;
      if($query->rowCount() > 0)
      {
       foreach($results as $result)
        {    ?>
         
         <tbody>
          <tr>
           <th scope="row"><?php echo htmlentities($cnt);?></th>
           <td><?php echo htmlentities($result->VehiclesTitle);?></td>
           <td><button type="button" class="btn btn-warning"><a href="view-details.php?id=<?php echo $result->id;?>">View Details</a></button></td>
           <td><button type="button" class="btn btn-danger"><a href="view-vehicle.php?del=<?php echo $result->id;?>" onclick="return confirm('Do you want to delete');">Deleted</a></button></td>
          </tr>
          <?php $cnt=$cnt+1; }} ?>
         </tbody>
        </table>
       </div>
      </body>
      </html>
      <?php } ?> 

Yang terakhir kalia harus membuat file dengan view-details.php file ini mempunyai fungsi untuk menampilkan details gambar yang ada di database.



 <?php
session_start();
error_reporting(0);
include('config.php');
if(strlen($_SESSION['alogin'])==0)
{ 
 header('location:index.php');
}


?>
<html>
<head>
 <title>Edit Image</title>
 <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
 <script src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
 <nav class="navbar navbar-default navbar-static-top">
  <div class="container-fluid">
   <div class="navbar-header">
    <a class="navbar-brand" href="https://www.tutorialswb.com/">TutorialsWB</a>
    <ul class="nav navbar-nav">
     <li class="active"><a href="index.php">Home</a></li>
    </ul>
   </div>
  </div>
 </nav>
 <div class="container">
  <div class="page-header">
   <h1 class="h2">&nbsp; List Users<a class="btn btn-success" href="view-vehicle.php" style="margin-left: 770px;"><span class="glyphicon glyphicon-user"></span>&nbsp; Back View Vehicle</a></h1><hr>
  </div>
  
  
  <?php if($msg){?>
   <div class="alert alert-success">
    
    <span class="glyphicon glyphicon-info-sign"></span>SUCCESS : <strong><?php echo htmlentities($msg); ?></strong>
    </div><?php }?>
    
    <form method="post" enctype="multipart/form-data">
     <?php
     $id=intval($_GET['id']);

     $sql = "SELECT * from  tblvehicles where tblvehicles.id=:id";
     $query = $dbh -> prepare($sql);
     $query-> bindParam(':id', $id, PDO::PARAM_STR);
     $query->execute();
     $results=$query->fetchAll(PDO::FETCH_OBJ);
     $cnt=1;
     if($query->rowCount() > 0)
     {
      foreach($results as $result)
       { ?>
        
        
        <div class="form-group">
         <label for="vehicletitle">Enter Your New Title Vehicle</label>
         <input type="text" name="vehicletitle" class="form-control" value="<?php echo htmlentities($result->VehiclesTitle)?>" readonly>
        </div>
        
        <div class="form-group">
         <div class="col-sm-4">
          Image 1 <img src="img/vehicleimages/<?php echo htmlentities($result->Vimage1);?>" width="300" height="200" style="border:solid 1px #000">
         </div>
         <div class="col-sm-4">
          Image 2<img src="img/vehicleimages/<?php echo htmlentities($result->Vimage2);?>" width="300" height="200" style="border:solid 1px #000">
         </div>
         <div class="col-sm-4">
          Image 3<img src="img/vehicleimages/<?php echo htmlentities($result->Vimage3);?>" width="300" height="200" style="border:solid 1px #000">
         </div>
        </div>
        


        <div class="form-group">
         <div class="col-sm-4">
          Image 4<img src="img/vehicleimages/<?php echo htmlentities($result->Vimage4);?>" width="300" height="200" style="border:solid 1px #000">
         </div>
         <div class="col-sm-4">
          Image 5
          <?php if($result->Vimage5=="")
          {
           echo htmlentities("File not available");
          } else {?>
           <img src="img/vehicleimages/<?php echo htmlentities($result->Vimage5);?>" width="300" height="200" style="border:solid 1px #000">
          <?php } ?>
         </div>
        </div>
        
       <?php } ?>
       

      </form>
     </div>
    </body>
    </html>
    <?php } ?> 

kalian bisa melihat demo dan bisa langsung mendownload filenya supaya kalian bisa lebih mencoba dan mempelajari sendiri.

DEMO     DOWNLOAD

Sekian share kali ini semoga apa yang kami share kali ini bermanfaat bagi anda jangan lupa share keteman dan jangan segang untuk meninggalkan komentar dibawah dan mulai berdiskusi dengan kami.


Related Post

SUBSCRIBE TO OUR NEWSLETTER

closed