Skip to content

Commit

Permalink
change use default upload path
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmunir committed Nov 7, 2016
1 parent f431814 commit a3a9b5b
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions FileModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
*/
class FileModel extends \yii\db\ActiveRecord
{
/**
* @var string
*/
public static $defaultUploadPath = '@runtime/upload';
/**
* @var integer
*/
public static $defaultDirectoryLevel = 1;
/**
* @var UploadedFile
*/
Expand All @@ -25,15 +33,15 @@ class FileModel extends \yii\db\ActiveRecord
/**
* @var string Upload path
*/
public $uploadPath = '@runtime/upload';
public $uploadPath;

/**
* @var integer the level of sub-directories to store uploaded files. Defaults to 1.
* If the system has huge number of uploaded files (e.g. one million), you may use a bigger value
* (usually no bigger than 3). Using sub-directories is mainly to ensure the file system
* is not over burdened with a single directory having too many files.
*/
public $directoryLevel = 1;
public $directoryLevel;

/**
* @var \Closure
Expand All @@ -56,22 +64,22 @@ public function rules()
return [
[['file'], 'required'],
[['file'], 'file', 'skipOnEmpty' => false],
[['uploadPath'], 'required', 'when' => function($obj) {
return empty($obj->filename);
}],
[['uploadPath'], 'default', 'value' => static::$defaultUploadPath],
[['name', 'size'], 'default', 'value' => function($obj, $attribute) {
return $obj->file->$attribute;
}],
[['type'], 'default', 'value' => function() {
return FileHelper::getMimeType($this->file->tempName);
}],
[['filename'], 'default', 'value' => function() {
$level = $this->directoryLevel === null ? static::$defaultDirectoryLevel : $this->directoryLevel;
$key = md5(microtime() . $this->file->name);
$base = Yii::getAlias($this->uploadPath);
if ($this->directoryLevel > 0) {
for ($i = 0; $i < $this->directoryLevel; ++$i) {
if (($prefix = substr($key, $i + $i, 2)) !== false) {
if ($level > 0) {
for ($i = 0; $i < $level; ++$i) {
if (($prefix = substr($key, 0, 2)) !== false) {
$base .= DIRECTORY_SEPARATOR . $prefix;
$key = substr($key, 2);
}
}
}
Expand Down

0 comments on commit a3a9b5b

Please sign in to comment.