Skip to content

Instantly share code, notes, and snippets.

@Kichrum
Created July 28, 2014 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kichrum/49de9b5da2d560de46aa to your computer and use it in GitHub Desktop.
Save Kichrum/49de9b5da2d560de46aa to your computer and use it in GitHub Desktop.
Get file path by file name in database - Yii
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