如何一次取消多个图像的链接?

时间:2014-01-02 03:29:26

标签: php mysql unlink

我有这个代码,它将从我的数据库中删除带有类别名称的图像,但它只取消链接我的图像文件夹中的一个图像,但我需要它一次取消链接多个图像,任何人都可以帮助这里是我的代码的一个例子。

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());

    } 

}

2 个答案:

答案 0 :(得分:2)

尝试以下代码并确保从数据库中获取包含图像名称的右列。

 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());

} 

}

答案 1 :(得分:1)

试试这个,

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

    $image = $row['img'];


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