yii\web\UploadedFile::getInstanceByName PHP Метод

getInstanceByName() публичный статический Метод

The name can be a plain string or a string like an array element (e.g. 'Post[imageFile]', or 'Post[0][imageFile]').
public static getInstanceByName ( string $name ) : null | UploadedFile
$name string the name of the file input field.
Результат null | UploadedFile the instance of the uploaded file. Null is returned if no file is uploaded for the specified name.
    public static function getInstanceByName($name)
    {
        $files = self::loadFiles();
        return isset($files[$name]) ? new static($files[$name]) : null;
    }

Usage Example

 /**
  * @inheritdoc
  */
 public function run()
 {
     if (Yii::$app->request->isPost) {
         $file = UploadedFile::getInstanceByName($this->uploadParam);
         $model = new DynamicModel(compact($this->uploadParam));
         $model->addRule($this->uploadParam, 'file', ['maxSize' => $this->maxSize, 'tooBig' => Yii::t('upload', 'TOO_BIG_ERROR', ['size' => $this->maxSize / (1024 * 1024)]), 'extensions' => explode(', ', $this->extensions), 'checkExtensionByMimeType' => false, 'wrongExtension' => Yii::t('upload', 'EXTENSION_ERROR', ['formats' => $this->extensions])])->validate();
         if ($model->hasErrors()) {
             $result = ['error' => $model->getFirstError($this->uploadParam)];
         } else {
             $model->{$this->uploadParam}->name = uniqid() . '.' . $model->{$this->uploadParam}->extension;
             $request = Yii::$app->request;
             $image_name = $this->temp_path . $model->{$this->uploadParam}->name;
             $image = Image::crop($file->tempName . $request->post('filename'), intval($request->post('w')), intval($request->post('h')), [$request->post('x'), $request->post('y')])->resize(new Box($this->width, $this->height))->save($image_name);
             // watermark
             if ($this->watermark != '') {
                 $image = Image::watermark($image_name, $this->watermark)->save($image_name);
             }
             if ($image->save($image_name)) {
                 // create Thumbnail
                 if ($this->thumbnail && ($this->thumbnail_width > 0 && $this->thumbnail_height > 0)) {
                     Image::thumbnail($this->temp_path . $model->{$this->uploadParam}->name, $this->thumbnail_width, $this->thumbnail_height)->save($this->temp_path . '/thumbs/' . $model->{$this->uploadParam}->name);
                 }
                 $result = ['filelink' => $model->{$this->uploadParam}->name];
             } else {
                 $result = ['error' => Yii::t('upload', 'ERROR_CAN_NOT_UPLOAD_FILE')];
             }
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException(Yii::t('upload', 'ONLY_POST_REQUEST'));
     }
 }
All Usage Examples Of yii\web\UploadedFile::getInstanceByName