mdm\upload\FileModel::saveAs PHP Method

saveAs() public static method

Save file
public static saveAs ( UploadedFile | string $file, array $options = [] ) : boolean | static
$file yii\web\UploadedFile | string
$options array
return boolean | static
    public static function saveAs($file, $options = [])
    {
        if (!$file instanceof UploadedFile) {
            $file = UploadedFile::getInstanceByName($file);
        }
        $options['file'] = $file;
        $model = new static($options);
        return $model->save() ? $model : false;
    }

Usage Example

 /**
  * Save uploaded file into [[$uploadPath]]
  * @param boolean $deleteOldFile If true and file exists, file will be deleted.
  * @return boolean|null if success return true, fault return false.
  * Return null mean no uploaded file.
  */
 public function saveUploadedFile($deleteOldFile = null)
 {
     /* @var $file UploadedFile */
     $file = $this->{$this->attribute};
     if ($file !== null) {
         $callback = $this->saveCallback;
         if ($callback !== null && is_string($callback)) {
             $callback = [$this->owner, $callback];
         }
         $model = FileModel::saveAs($file, ['uploadPath' => $this->uploadPath, 'directoryLevel' => $this->directoryLevel, 'saveCallback' => $callback]);
         if ($model) {
             if ($this->savedAttribute !== null) {
                 if ($deleteOldFile === null) {
                     $deleteOldFile = $this->deleteOldFile;
                 }
                 $oldId = $this->owner->{$this->savedAttribute};
                 $this->owner->{$this->savedAttribute} = $model->id;
                 if ($deleteOldFile && ($oldModel = FileModel::findOne($oldId)) !== null) {
                     return $oldModel->delete();
                 }
             }
             return true;
         }
         return false;
     }
 }
All Usage Examples Of mdm\upload\FileModel::saveAs