I'm making the gallery website and would like to create a multiple image uploader using PHP and MYSQLI only... I'm not really good in coding so the other examples of multiple image upload on this site didn't work for me.
Here is the working code which posts the data into database according to the current user session.
html:
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="image">Select a file:</label>
<input type="file" name="image[]" id="image" multiple='multiple' />
<br /><br>
<label for="picname">Title:</label>
<input type="text" name="picname" id="picname" />
<br /><br>
<label for="picdesc">Description:</label>
<input type="text" name="picdesc" id="picdesc" />
<br /><br>
<label for="piccat">Category:</label>
<select name="piccat" id="piccat">
<option value="--"></option>
<option value="animation">Animation</option>
<option value="illustration">Illustration</option>
<option value="photography">Photography</option>
</select>
<br /><br>
<input type="submit" name="submit" value="Submit" />
</form>
php & mysqli:
<?php
$path = "images/projects/";
include("check.php");
if (isset($_POST["submit"])) {
$image = $_FILES["image"]["tmp_name"];
$imageName = $_FILES["image"]["name"];
$imageSize = $_FILES["image"]["size"];
$imageType = $_FILES["image"]["type"];
$imageTitle = $_POST["picname"];
$imageDescription = $_POST["picdesc"];
$imageCategory = $_POST["piccat"];
$len = count($image);
$path = $path . $imageName;
$query = $db -> prepare("INSERT INTO images (user_id, image, description, type, title, size, category)
VALUES (?, ?, ?, ?, ?, ?, ?)");
$query -> bind_param('issssis', $_SESSION['user_id'], $imageName, $imageDescription, $imageType, $imageTitle, $imageSize, $imageCategory);
$query -> execute();
$query -> close();
if ($imageName){
move_uploaded_file($image, $path);
}
}
?>
It works fine but only uploads one image. How to select multiple images and upload all of them at once?