user2004096
user2004096

Reputation: 25

delete images through database and directory

can somebody help me for my codes. i can delete the image in the database but in the directory i can't. im tried for long hours but it seems not work at all. would somebody help me please? here's my code: this is the code where the images

<?
//this is were images displayed
                    $query = "SELECT * FROM images WHERE category='home'";
                    $result = mysql_query($query) or die(mysql_error());
                    while($row = mysql_fetch_array($result)){
                    ?>
                    <a href="edithomephotos.php?delete=<?=$row['imageID']?>" onclick = "return confirm('Are you sure you want to delete?')"><img src="images/template/delete.png" id="AEDbutton"></a>


                    echo "<img border=\"0\" src=\"".$row['image']."\" width=\"200\"  height=\"100\">";
                    echo "<br>";                }   


?>

,?

include('global.php');

//this is were image were deleted

if($delete != "") 



    {

        $query = "DELETE FROM images WHERE imageID='".$delete."'";
        ExecuteQuery($query);   

}

//but in here , it cannot delete image through directory
           $query = "SELECT * FROM images WHERE imageID='".$delete."'";

                $result = mysql_query($query);

while ($delete = mysql_fetch_array($result)) {
                $image = $delete['image'];

                 $file= '/.directory/'.$image;

                        unlink($file);


    }



?>

Upvotes: 0

Views: 15264

Answers (3)

Chethan Gowda
Chethan Gowda

Reputation: 101

If you are deleting images, first you need to delete image and then delete in DB

Upvotes: 0

Andrei Cristian Prodan
Andrei Cristian Prodan

Reputation: 1122

Funny. That's because you are first deleting the image id from the database and after that you are trying to get the ID of the previously deleted image (which no longer exists) and delete the file associated with it. Switch the code like this.

include('global.php');

if($delete != "") 
{
//first delete the file
$query = "SELECT * FROM images WHERE imageID='".$delete."'";

$result = mysql_query($query);

while ($delete = mysql_fetch_array($result))
{
    try
    {
        $image = $delete['image'];
        $file= '/images/'.$image;
        unlink($file);
    } catch (Exception $e) {

    }
}

// after that delete the id from the db of that image associated with the deleted file
$query = "DELETE FROM images WHERE imageID='".$delete."'";
ExecuteQuery($query);   
}

UPDATE: I added a try catch

Upvotes: 1

Ramesh Moorthy
Ramesh Moorthy

Reputation: 689

You already deleted the image entry in table, after that you try to get the same entry in DB. so, first you can delete the image from folder after that you can delete in the table.

<?php
    include('global.php');

    if($delete != "") {
        $query = "SELECT * FROM images WHERE imageID='".$delete."'";
        $result = mysql_query($query);

        while ($delete = mysql_fetch_array($result)) {
            $image = $delete['image'];
            $file= '/.directory/'.$image;
            unlink($file);
        }

        $query = "DELETE FROM images WHERE imageID='".$delete."'";
        ExecuteQuery($query);   
    }
?>

Note: Make sure your path $file= '/.directory/'.$image; is correct, I think it referring from root directory.

Upvotes: 2

Related Questions