Multiple Images Uploads Step by Step

Lets see with example

  

  <input type="file" name="image[]" id="form-field-2" placeholder="Select News Image For Uploading" class="col-xs-10 col-sm-5" required multiple="multiple" />
  <input type="file" name="image[]" id="form-field-2" placeholder="Select News Image For Uploading" class="col-xs-10 col-sm-5" required multiple="multiple" />

  <?php
  include "connection.php";

  if (isset($_POST['submit'])) {
    extract($_POST);
    $i = 0;
    $total = count($_FILES['image']['name']);

    for ($i = 0; $i < $total; $i++) {

      $tmpFilePath = $_FILES['image']['tmp_name'][$i];
      if ($tmpFilePath != "") {

        $img = md5(time());
        $newFilePath = "" . $img . $_FILES['image']['name'][$i];
        if (move_uploaded_file($tmpFilePath, "../event/" . $newFilePath)) {

          $query = mysqli_query($con, "insert into event_photo(ep_e_id,ep_photo) values ('$title','$newFilePath')") or die(mysqli_error($con));

          if ($query == 1) {

            echo '<script type="text/javascript">';

            echo 'alert("Event Image Uploaded Successfully...");';
            echo 'window.location.href = "add_event_photo.php";';
            echo '</script>';
          } else {
            echo "" . mysqli_error($con);
          }
        } elseif (isset($_POST['cancel'])) {

          echo '<script type="text/javascript">';


          echo 'window.location.href = "add_event_photo.php";';

          echo '</script>';
        }
      }
    }
  }
  ?>

  <?php
  include "connection.php";

  if (isset($_POST['submit'])) {
    extract($_POST);
    $i = 0;
    $total = count($_FILES['image']['name']);

    for ($i = 0; $i < $total; $i++) {

      $tmpFilePath = $_FILES['image']['tmp_name'][$i];
      if ($tmpFilePath != "") {

        $img = md5(time());
        $newFilePath = "" . $img . $_FILES['image']['name'][$i];
        if (move_uploaded_file($tmpFilePath, "../event/" . $newFilePath)) {

          $query = mysqli_query($con, "insert into event_photo(ep_e_id,ep_photo) values ('$title','$newFilePath')") or die(mysqli_error($con));

          if ($query == 1) {

            echo '<script type="text/javascript">';

            echo 'alert("Event Image Uploaded Successfully...");';
            echo 'window.location.href = "add_event_photo.php";';
            echo '</script>';
          } else {
            echo "" . mysqli_error($con);
          }
        } elseif (isset($_POST['cancel'])) {

          echo '<script type="text/javascript">';
          echo 'window.location.href = "add_event_photo.php";';
          echo '</script>';
        }
      }
    }
  }


Comments

Post a Comment

Popular posts from this blog

Simple PHP Mysql Shopping Cart

How to seperate character from string in php

How to Delete record using PHP Ajax