Search code examples
phpmysqlunlink

Delete image row and delete image from directory


As the title states I'm trying to delete the image row information from the database and delete the actual image from the gallery directory.

If I run this it deletes the row from my database:

$id = intval($_GET['id']);

$deletepic = DB::getInstance()->delete('gallery', array(
    'id', '=', $id
));

If I do the following it will delete the image from the directory, but will not delete the row.

$id = intval($_GET['id']);
$gallery = DB::getInstance()->query("SELECT `file_name` FROM `gallery` WHERE ID = $id");

foreach($gallery->results() as $file_name){

$pic =  ($file_name->file_name);
}

$deletepics = DB::getInstance()->delete('gallery', array(
    'id', '=', $id,
    unlink('../../images/gallery/'.$pic)
));

I can't understand how to get the name of the image without running the select query, but I need to run it like that so I can unlink the image.

Can someone help me combined the delete row and unlink function here?

Any advice would be appreciated.


Solution

  • This should work

    $deletepics = DB::getInstance()->delete('gallery', array('id', '=', $id));
    unlink('../../images/gallery/'.$pic);