Created
July 28, 2014 13:55
-
-
Save Kichrum/49de9b5da2d560de46aa to your computer and use it in GitHub Desktop.
Get file path by file name in database - Yii
This file contains hidden or 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
class File extends CActiveRecord | |
{ | |
public function rules() | |
{ | |
array('filename', 'file', 'types'=>'pdf, doc, docx, xls, xlsx, ods, odt, zip, rar, avi, mp4, flv, txt, webm', 'allowEmpty' => true), | |
); | |
} | |
/** | |
* Get file path by file name in database | |
* | |
* @param boolean $validate whether is needed to check if file exists | |
* @return string path to file | |
*/ | |
public function getFilePath($validate = true) | |
{ | |
$path = null; | |
if(!$validate || !empty($this->filename)) | |
{ | |
$uploadsPath = Yii::getPathOfAlias('application.uploads'); | |
$filePath = $uploadsPath . DIRECTORY_SEPARATOR . $this->filename; | |
if(!$validate || file_exists($filePath)) | |
{ | |
$path = $filePath; | |
} | |
} | |
return $path; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment