Uploading multiple files to one post

  html, php, uploading

I’m trying to create a gallery site, posts should have one main image displayed on the left and 3 smaller images to the right of it. I don’t know how to let the user upload those 3 smaller pictures. Here’s the PHP for the post:

<?php
  include_once "includes/dbh.php";

  $sql = "SELECT * FROM gallery ORDER BY orderGallery DESC;";
  $stmt = mysqli_stmt_init($conn);
  if (!mysqli_stmt_prepare($stmt, $sql)) {
    echo "SQL error!";
  }
  else {
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    while ($row = mysqli_fetch_assoc($result)) {
      echo '<div class="post">
                <div class="image">
                  <img src="img/gallery/' . $row["imgFullNameGallery"] . '">
                </div>  
              <span><h3>' . $row["titleGallery"] . '<h3></span>
              <span><p>' . $row["descGallery"] . '</p></span>
              <span><div class="row">
                <div class="column">
                  <img src="img/gallery/SMALL PICTURE NUMBER 1" style="width:100%">
                </div>
                <div class="column">
                  <img src="img/gallery/SMALL PICTURE NUMBER 2" style="width:100%">
                </div>
                <div class="column">
                  <img src="img/gallery/SMALL PICTURE NUMBER 3" style="width:100%">
                </div>
              </div></span>
            </div>';
    }
  }
?> 

Here’s the PHP for uploading (only when the user is logged in):

<?php
  if (isset($_SESSION['loggedin'])) {
    echo'<div class="gallery-upload">
      <form class="form-signup" action="includes/gallery-upload.php" method="post" enctype="multipart/form-data">
        <input type="text" name="filename" placeholder="File name (optional)">
        <input type="text" name="filetitle" placeholder="Name">
        <input type="text" name="filedesc" placeholder="Description (optional)">
        <input type="file" name="file"></li>
        <button type="submit" name="submit">Submit</button>
      </form>
    </div>';
  }
?>

and here’s the gallery-upload.php

<?php

if (isset($_POST['submit'])) {

  $newFileName = $_POST['filename'];
  if (empty($newFileName)) {
    $newFileName = "NewFile";
  } else {
    $newFileName = strtolower(str_replace(" ", "-", $newFileName));
  }
  $imageTitle = $_POST['filetitle'];
  $imageDesc = $_POST['filedesc'];

  $file = $_FILES['file'];

  $fileName = $file["name"];
  $fileType = $file["type"];
  $fileTempName = $file["tmp_name"];
  $fileError = $file["error"];
  $fileSize = $file["size"];

  $fileExt = explode(".", $fileName);
  $fileActualExt = strtolower(end($fileExt));

  $allowed = array("jpg", "jpeg", "png");

  if (in_array($fileActualExt, $allowed)) {
    if ($fileError === 0) {
      if ($fileSize < 200000000) {
        $imageFullName = $newFileName . "." . uniqid("", true) . "." . $fileActualExt;
        $fileDestination = "../img/gallery/" . $imageFullName;

        include_once "dbh.inc.php";

        // można dodać || empty($imageDesc)
        if (empty($imageTitle)) {
          header("Location: index.php?upload=empty");
          exit();
        } else {
          $sql = "SELECT * FROM gallerytable;";
          $stmt = mysqli_stmt_init($conn);
          if (!mysqli_stmt_prepare($stmt, $sql)) {
            echo "SQL error";
          } else {
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
            $rowCount = mysqli_num_rows($result);
            $setImageOrder = $rowCount + 1;

            $sql = "INSERT INTO gallerytable (titleGallery, descGallery, imgFullNameGallery, orderGallery) VALUES (?, ?, ?, ?);";
            if (!mysqli_stmt_prepare($stmt, $sql)) {
              echo "SQL error";
            } else {
              mysqli_stmt_bind_param($stmt, "ssss", $imageTitle, $imageDesc, $imageFullName, $setImageOrder);
              mysqli_stmt_execute($stmt);

              move_uploaded_file($fileTempName, $fileDestination);

              header("Location: ../index.php?upload=success");
            }
          }
        }
      } else {
        echo "File size is too big";
        exit();
      }
    } else {
      echo "Error!";
      exit();
    }
  } else {
    echo "Error!";
    exit();
  }
}
?>

Database table:

CREATE TABLE gallerytable (
  idGallery int(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,
  titleGallery LONGTEXT NOT NULL,
  descGallery LONGTEXT NOT NULL,
  imgFullNameGallery LONGTEXT NOT NULL,
  orderGallery LONGTEXT NOT NULL
);

Source: Ask PHP

LEAVE A COMMENT