Skip to content

Instantly share code, notes, and snippets.

@maximishchenko
Last active September 17, 2015 18:30
Show Gist options
  • Save maximishchenko/b4426a21ef9d5ae6c7a4 to your computer and use it in GitHub Desktop.
Save maximishchenko/b4426a21ef9d5ae6c7a4 to your computer and use it in GitHub Desktop.
Yii delete uploaded file when delete record from database
<?php
// Add to current model
protected function beforeDelete()
{
if (parent::beforeDelete()) {
// file_field_name - db field that contains name of uploaded file
if ($this-><file_field_name>)
// webroot.images - path to images forlder (such as Yii::app()->baseurl."/images/")
// if using another forder name (for example: /upload/) - Yii::getPathOfAlias('webroot.upload')
@unlink(Yii::getPathOfAlias('webroot.images') . "DIRECTOTY_SEPARATOR" . $this-><file_field_name>);
return true;
} return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment