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 http://pastebin.com/KQZyAuA0 for the update and delete actions, if anyone knows the answer then kindly share with me.

Thanks in advance


  $uploadedFile->saveAs(Yii::app()->basePath.'/../images/gallery/frontpageslider/'.$name);

//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                   

 @unlink(Yii::app()->basePath.'/../images/gallery/frontpageslider/'.$photo);                    

@unlink(Yii::app()->basePath.'/../images/gallery/frontpageslider/resize/'.$photo);

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.

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#beforeDelete-detail

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 ){


                    Image::model()->deleteAll('product_id='.$id);


                    foreach ($images as $image) {


                        if(file_exists("images/listing_images/".$image->filename))


                                unlink("images/listing_images/".$image->filename);


                        if(file_exists("images/listing_images/Thumbs/".$image->filename))


                                unlink("images/listing_images/Thumbs/".$image->filename);


                        if(file_exists("images/listing_images/gallery/".$image->filename))


                                unlink("images/listing_images/gallery/".$image->filename);


                    }


            }


     $this->loadModel($id)->delete();





   }

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)) {

                unlink($oldThumb);

            }

            if (file_exists($oldOptimized)) {

                unlink($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




<?php


// helper function just to keep things simple

private function deleteFile($file)

{

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

	if (file_exists($file)) {

        unlink($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.