I am developing a site which allow uploading multiple images for one id. But I want to upload images into text format into database and real image to my directory. But before uploading image into database I preview images using JavaScript's FileReader.
My problem is, when I click remove in preview image , that image should also be removed from images that I want to upload. And second, that I don't know how to do this, when I click first time and select some images, then again I select some images by clicking input field, the images selected second time is being uploaded into database instead of all.
Here are my codes
// image preview
$("#productImage").on("change", function(e) {
var files = e.target.files,
filesLength = files.length;
for (var i = 0; i < filesLength; i++) {
var f = files[i]
var fileReader = new FileReader();
fileReader.onload = (function(e) {
var file = e.target;
$('.image-preview').append("<div class=\"product-image\">" +
"<img class=\"image-thumb\" src=\"" + e.target.result +
"\" title=\"" + file.name + "\" + data-file = \"" + file.name +
"\"/>" +
"<br/><div class=\"remove-image\">❌ Remove</span>" +
"</div>");
$('.remove-image').click(function(e) {
$(this).parent('.product-image').remove();
});
});
fileReader.readAsDataURL(f);
}
});
.form-elements {
display: flex;
flex-direction: column;
justify-content: left;
padding: 10px 0;
}
.form-elements .input-label {
padding: 10px 0;
}
.input-label label {
font-family: sans-serif;
font-size: 20px;
color: #fff;
}
.image-preview {
display: flex;
flex-wrap: wrap;
padding: 10px 0;
margin-bottom: 5px;
}
.image-preview .product-image {
display: block;
margin: 5px 10px;
width: 150px;
text-align: center;
}
.image-preview .product-image .image-thumb {
width: 100%;
border-radius: 10px;
cursor: pointer;
}
.image-preview .product-image .remove-image {
padding: 5px;
margin: 5px 0;
border-radius: 10px;
font-family: sans-serif;
font-size: 15px;
background: #ff3636;
color: #fff;
cursor: pointer;
}
.image-preview .product-image .remove-image:hover {
background: red;
}
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<!-- product images -->
<div class="form-elements outside">
<div class="input-label">
<label for="productName">Select product image(s)</label>
</div>
<div class="image-preview">
<!-- Images will be here -->
</div>
<input type="file" id="productImage" name="productImage[]" multiple />
</div>
PHP code snippet
<?php
//connection
$connect = mysqli_connect('localhost','root','','sahi_chuno_db');
$image = $_FILES['productImage']['name'];
$temp_image = $_FILES['productImage']['tmp_name'];
$product_id = 1;
for ($i=0; $i < count($image); $i++) {
$query = $connect->prepare("INSERT INTO `product_images` (`product_id`, `product_image`)
VALUES(?, ?)");
$query -> bind_param('is',$product_id, $image[$i]);
$run = $query -> execute();
if($run){
//move images to directory
move_uploaded_file($temp_image[$i], "../uploads/$image[$i]");
} else{
echo "Not uploaded";
}
}
?>