How Delete Article Images?


How to delete the article images when is confirmed the article deletion?

Any ideas ?


You could try to use unlink.

try this

$folder = YiiBase::getPathOfAlias('webroot') . '/upload/event/' . $_POST['imagename'];

            if (file_exists($folder) && $_POST['imagename'] != "") {


                @unlink(YiiBase::getPathOfAlias('webroot') . '/upload/event/thumb81/' . $_POST['imagename']);



I hope it’s some help.

Thx …

But main is more simple …

                         $Article = $this->loadModel($id);




But the problem is bigger :(

The table include col name "cid" related to category, my code delete all the articles & image which under one "cid" ??!

How to debug this?


I’m not sure I understand you. You want to delete all of the articles and images with a particular category id? Your code looks like it should work just fine to delete one article and its image.

If you do want to delete articles and images that belong to a specific category, you could try the following.

$articles = Article::model()->findAllByAttributes(array('cid'=>$someCategoryId));

foreach($articles as $article){





You could also try a custom query instead of creating all those model objects, get the main_image of each article, and then delete both the images and the articles.

What I mean my code is deleting all articles which is have the cid as the wanted article.

Hi marvix,

It sounds like you have some error in your handling of the related objects.

Don’t you try to remove the category in your Article::afterDelete()?


I do want to remove the category, the delete funciton is delete it all the records with have same category ID when am deleting one record.

The function afterDelete does not exist in the model …

The form code:

echo CHtml::ajaxLink('delete',



    												                            'data' => array('id' =>$model->id,'type'=>'delete'),

    												                            'success' => "function() {




    												                            	array(	'confirm'=>'Are you sure?', 

    												                            			"class"=>'btn btn-danger btn-small pull-left moa'


The delete function in the controller:

public function actionDelete($id)





                         $Article = $this->loadModel($id);




             // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser


                                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));



                        throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');



2 days spent on this error …

Also tired with this code:

                         $Article = $this->loadModel($id);

                         if ( $Article->deleteByPk($id) ) {




and its the same …

strange … deleting one record from db with sql query :

DELETE FROM `article` WHERE `article`.`id`='7802'

result deleting all records which have same cid !!

could this because of relations & foreign key ?

Probably you have set up a wrong FK constraint, or a bad trigger to delete a record in category table.

I found it yesterday … it was cascade on delete on child level, delete it one record will delete all records with same CID as cascaded …

Thanks for being helpful ;)