Hi to all, I have a model for Items. I this model I have a relationship to another model Images where I save the path to the item images in which an item can have as many images.
When an item is deleted I need to delete the path in the image table and this works fine but I need to delete the file(image) of the item too. Here is my code but is not deleting the file which is the image using the unlink function. I might be missing something please help.
protected function afterDelete()
{
parent::afterDelete();
ItemRecommendations::model()->deleteAll('item_id=' .$this->id);
ItemTag::model()->updateFrequency($this->tags, '');
//Delete images location
$image_path = Yii::app()->getBasePath().'/../../uploads/item/';
//query table where id = current id
$item_data = ItemImage::model()->findAll('item_id =' .$this->id);
if(!empty($item_data->image_path))
unlink($image_path.$item_data->image_path);
unlink($image_path.$item_data->image_thumb_path);
//Delete all the image path for the current item
ItemImage::model()->deleteAll('item_id =' .$this->id);
}
I tried your method the only thing it does is deleting the value of the image path in the database but not the image file from the folder am uploading the images. I need it to delete the item image from the folder am uploading.
That’s identical to what I do - are you absolutely sure your paths are right? you’re going relative … so that might be a culprit. I would prefer doing something ( in my top level index )
Yii::app()->params[‘webroot’] = getcwd();
so you can do ( if you’re images are held in the web root )
Thanks notsoluckycharm for your help.I have my upload folder at the same level as yii default index.php. The folder is in the format uploads/items and in my image table I have as example for image_path column I have upload/items/image1.jpg and for the thumb I have image_thumb_path upload/items/image1_thumb.jpg. Also what is getcwd() in your example. Thinks seem not to work. The path is deleted from the table fine but the image is not being removed from the folder.
Bonnie, can you show me your complete code (model, controller) to delete file because I have the same problem. To remove I use standard solutions of yii.