user3152355
user3152355

Reputation: 21

How can I unlink multiple images at once?

I have this code which will delete images with the category name from my database and but it only unlink one image from my images folder but I need it to unlink multiple images at once can anyone help here is an example of my code.

if(isset($_GET['delete'])) {

    $delete_id = $_GET['delete'];

    $sql = "SELECT image FROM images WHERE category = '$delete_id'";

    $query = mysqli_query($connection,$sql) or die (mysqli_error());

    while ($row = mysqli_fetch_array($query)){

        $image = $row['image'];

        $location_full_image = "../images/$image";

        $location_thumb_image = "../images/thumbnails/$image";

        @unlink($location_full_image); 

        @unlink($location_thumb_image); 

        $sql = "DELETE FROM images WHERE category = '$delete_id'";

        $query = mysqli_query($connection,$sql) or die (mysqli_error());

    } 

}

Upvotes: 1

Views: 3780

Answers (2)

Adarsh Nahar
Adarsh Nahar

Reputation: 319

Try this,

while ($row = mysqli_fetch_array($query)){

    $image = $row['img'];


   @unlink("images/".$image);
}

Upvotes: 1

King-of-IT
King-of-IT

Reputation: 578

try below code and make sure you are fetching right column from database which contains image name.

 if(isset($_GET['delete'])) {

$delete_id = $_GET['delete'];

$sql = "SELECT image FROM images WHERE category = '$delete_id'";

$query = mysqli_query($connection,$sql) or die (mysqli_error());

while ($row = mysqli_fetch_array($query)){

    $image = $row['image'];

    @unlink('../images/'.$image); 

    @unlink('../images/thumbnails/'.$image); 

    $sql = "DELETE FROM images WHERE category = '$delete_id'";

    $query = mysqli_query($connection,$sql) or die (mysqli_error());

} 

}

Upvotes: 2

Related Questions