How to delete an image permanently from the database and folder location when a record is updated or deleted

Hello. I am having an issue. I have an image option for my record, but when I update my record or delete the record then it just impact the database tables and the images files are still located in the folder location. I have read about unlink function of php, but don’t know how to use it in my case.

here is my code for the update and delete actions, if anyone knows the answer then kindly share with me.

Thanks in advance


//store the image in folder                    

$images = Yii::app()->image->load('images/gallery/frontpageslider/'.$name);            

$images -> resize(1070, 0);//resize the image      

$images->save(Yii::app()->basePath.'/../images/gallery/frontpageslider/'.$name);//store the image in folder                  

$images = Yii::app()->image->load('images/gallery/frontpageslider/'.$name);            

$images -> resize(110, 0);//resize the image                    

$images->save(Yii::app()->basePath.'/../images/gallery/frontpageslider/resize/'.$name);//store the image in folder                   



Easy, even easier in Yii…

just implement beforeDelete in you Photo model (assumption based on your code provided) will dlete all your images and thumbnails before deleting your record.

Or what I did is overwrite the actionDelete() like follows.

public function actionDelete($id)


            $images = Image::model()->findAll('product_id='.$id.' AND type="listing"');


            if(count($images)>0 ){


                    foreach ($images as $image) {











I am doing it like this

 $oldThumb = Yii::getPathOfAlias('media') . '/album/thumbnails/' . $model_old_data->photo;

        $oldOptimized = Yii::getPathOfAlias('media') . '/album/optimized/' . $model_old_data->photo;

        if ($model->save()) {

            if (file_exists($oldThumb)) {



            if (file_exists($oldOptimized)) {



            $this->redirect(array('view', 'id' => $model->id));


but the issue is that the image is optional mean if the user have entered the image then on update action the previous image file is deleted from the folder location and new image is saved in the folder, but if the user have not set any image for the record and then on the update action the unlink function gives error

unlink(C:\wamp\www\mysite\admin\protected\config\..\..\..\media/items/thumbnails/): Permission denied

what should I do with that ?

first of all I would refactor my code and extract the delete file logic into a helper like so


// helper function just to keep things simple

private function deleteFile($file)


	$file = Yii::getPathOfAlias('media') . $file;

	if (file_exists($file)) {




// then if the file is not empty delete the thumb and optimized image

// otherwise continue

if (!empty($model_old_data->photo)) {

	$this->deleteFile('/album/thumbnails/' . $model_old_data->photo);

	$this->deleteFile('/album/optimized/' . $model_old_data->photo);


if ($model->save()) {

    $this->redirect(['view', 'id' => $model->id]);


for this I did if(file_exists("images/listing_images/".$image->filename)), first we will verify file exist or not, even earlier you may check that DB field is not null than perform deletion.