Manish Tiwari Manish Tiwari - 3 months ago 40
PHP Question

How to unlink image and delete from database using codeigniter in php

I have upload images in rooturl + uploads/slider
and image name store in database slider_image.

I want to delete slider_image from databse and also delete from folder.

my folder location :

http://localhost/game/uploads/slider/soccer.jpg


image_name:

soccer.jpg

I got an warning error :


Severity: WarningMessage: unlink(): http does not allow unlinking


my model code:

public function deleteSlider($sliderID)
{
$this->db->delete('slider_tbl',array('slider_id' => $sliderID));
$path = base_url("uploads/slider/".$result[0]->slider_image);
if($this->db->affected_rows() >= 1){
if(unlink($path))
return TRUE;
} else {
return FALSE;
}
}

Answer

Try Changing your $path.

$path = base_url("uploads/slider/".$result[0]->slider_image);

to

$path = "./uploads/slider/" . $result[0]->slider_image;