I am a newbie. The main app is for house listings. Each house address is used(as filename) to store all images of a particular house in a folder. The folder path is then saved in the database. The below code is a part of the main app; it is for updating the folder in database(delete an image and update). I have searched the net to no avail. Help will be highly appreciated. Thanks.
- displayImage($imgs, $pathimg) is for displaying the images each with delete button.
- The javaScript myimageDelete() is working; removes the image from the screen.
- However, the php phpUnlink($pathimg, $pic) is not working. I am not able to unlink() the deleted image.
- I called the PHP function in the js function it did not work.
- I need help to unlink am image and update the folder and database
<?php
include './db.conn.php';
include './header.php';
?>
<?php
if (isset($_POST['save'])) {
$address = $_POST['img_addr1'];
$id = $_POST['prop_Id'];
if (isset($path)) {
$path1 = $pathimg;
$total_img = scandir($path1);
$sql = "UPDATE `list_tble` SET `img_addr`='$path1' WHERE prop_Id=$id";
$stmt = $conn->prepare($sql);
$stmt->execute();
}
}
?>
<div class=" d-flex flex-column mx-auto bg-secondary mb-2" style="width: 500px; height:300px;">
<div id="image_preview" class="bg-light w-100 h-20 d-flex flex-wrap overflow-auto mb-2" style="border: 6px solid grey">
<?php
displayImage($imgs, $pathimg);
?>
</div>
</div>
</div>
<?php
function displayImage($imgs, $pathimg)
{
if (count($imgs) > 2) {
foreach (array_splice($imgs, 2) as $pic) {
echo '<form method="POST" id="small_img" class="dis_Image bg-warning mx-auto d-flex flex-column" style="width: 120px; height: 120px;"> <img src ="' . $pathimg . '/' . $pic . '" width="120px" height="120px" style="border:2px solid white" alt="House list image" />
<input name="img_del" type="text" class="spanImage w-100 bg-danger fw-bold mx-auto"
style="cursor: pointer; color: white; font-size= 18px;"
onclick="myimageDelete()" value="DELETE" />
</form>';
}
} else {
echo "Directory is Empty.";
}
}
?>
<?php
function phpUnlink($pathimg, $pic)
{
if (isset($_POST['img_del'])) {
$imgToUnlink = $pathimg . '/' . $pic;
if (!unlink($imgToUnlink)) {
echo "$imgToUnlink is not deleted";
} else {
echo "$imgToUnlink is deleted";
}
}
}
?>
<script type='text/javascript'>
function myimageDelete() {
event.target.parentNode.remove();
}
</script>
<?php
include './footer.php';
?>