Images are not loading properly
I tried to create a gallery using php admins, you can upload multiple images, all images are loaded correctly as the image saves in the database as much as I can, but the problem is that the image is saved once in the folder
As in a loop, the filename is stored twice in the database
Forexampl img1.jpg img2.jpg img3.jpg
this is what comes in the database, but when i open the folder i see img1.jpg the rest do not come
here is my code
$imggall = $_FILES["gallery_img"]["name"];
$imgtype = $_FILES["gallery_img"]["type"];
$imgtemp = $_FILES["gallery_img"]["tmp_name"];
foreach($imggall as $key => $imgname) {
$path = "./img/gallery/".$imgname;
foreach($imgtemp as $key1 => $imgtemp1) {
move_uploaded_file($imgtemp1, $path);
}
$confirm = mysqli_query($connect, "INSERT INTO gallery (image) VALUES ('$imgname')");
}
Here is the HTML form for the post.php page
<p>
<form method="POST" action="post.php?id=<?php echo $_SESSION["id"]; ?>" enctype="multipart/form-data">
<div class="form-group">
<label>Title</label>
<input class="form-control" name="title">
</div>
<div class="form-group">
<label>Category</label>
<div class="checkbox">
<?php
$query = mysqli_query($connection, "SELECT DISTINCT cat_name FROM category");
while($row = mysqli_fetch_assoc($query)) {
if(!empty($row["cat_name"])) {
?>
<label><input type="checkbox" name='catname' value="<?php echo $row["cat_name"]; ?>"><?php echo $row["cat_name"]; ?></label>
<?php } } ?>
<?php
$query1 = mysqli_query($connection, "SELECT DISTINCT parent FROM category");
while($row1 = mysqli_fetch_assoc($query1)) {
if($row1["parent"] !== "None") {
?>
<label><input type="checkbox" name='catname' value="<?php echo $row1["parent"]; ?>"><?php echo $row1["parent"]; ?></label>
<?php } } ?>
</div>
</div>
<div class="form-group">
<label>Featured Image</label>
<input type="file" name="ft_img">
</div>
<div class="form-group">
<label>Description</label>
<textarea class="form-control" name="descrip" rows="6"></textarea>
</div>
<div class="form-group">
<label>Featured</label>
<div class="checkbox">
<label><input type="checkbox" name='fetch' value="Featured">Featured</label>
</div>
</div>
<div class="form-group">
<label>Android Button</label>
<select class="form-control" name="android">
<option>Yes</option>
<option>No</option>
</select>
</div>
<div class="form-group">
<label>Iphone Button</label>
<select class="form-control" name="iphone">
<option>Yes</option>
<option>No</option>
</select>
</div>
<div class="form-group">
<label>Custom Button Text</label>
<input class="form-control" name="cbtn_text">
</div>
<div class="form-group">
<label>Custom Button Link</label>
<input class="form-control" name="cbtn_url">
</div>
<script type="text/javascript">
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div class="form-group"><input type="file" name="gallery_img[]" style="float:left;" /><a href="#" class="remove_field"><i class="fa fa-times"></i></a><div class="clear"></div></div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
<div class="input_fields_wrap form-group">
<div class="form-group">
<button class="add_field_button btn btn-default">Add More Fields</button>
</div>
<div class="form-group">
<label>Gallery Images</label>
<input type="file" name="gallery_img[]">
<div class="clear"></div>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-default purple-btn" value="Submit" name="posts" />
<input type="Reset" class="btn btn-default purple-btn" value="Reset Value" />
<a href="cat.php?id=<?php echo $_SESSION['id'];?>" class="btn btn-danger">Cancel</a>
</div>
</form>
</p>
+3
source to share
1 answer
looping twice may have an impact on performance, this code works for me in my gallery
<?php
$files = $_FILES['gallery_img']['name'];
$fileCount = count($files);
for($i=0; $i<$fileCount; $i++) {
$tmpFilePath = $files[$i];
if ($tmpFilePath != ""){
$newFilePath = "./img/gallery/" . $files[$i];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
// if files are uploaded then only insert into database
$confirm = mysqli_query($connect, "INSERT INTO gallery (image) VALUES ('$files[$i]')");
}
}
}
?>
Hope this can work
+1
source to share