Last active
September 17, 2015 18:30
-
-
Save maximishchenko/b4426a21ef9d5ae6c7a4 to your computer and use it in GitHub Desktop.
Yii delete uploaded file when delete record from database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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