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

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

Returns all uploaded files for the given model attribute.
public static getInstances ( Model $model, string $attribute ) : UploadedFile[]
$model yii\base\Model the data model
$attribute string the attribute name. The attribute name may contain array indexes for tabular file uploading, e.g. '[1]file'.
Результат UploadedFile[] array of UploadedFile objects. Empty array is returned if no available file was found for the given attribute.
    public static function getInstances($model, $attribute)
    {
        $name = Html::getInputName($model, $attribute);
        return static::getInstancesByName($name);
    }

Usage Example

Пример #1
8
 /**
  * Creates a new Sucursal model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Sucursal();
     $model->create_user = Yii::$app->user->getId();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         $saved = true;
         $model->imagenes = UploadedFile::getInstances($model, 'imagenes');
         foreach ($model->imagenes as $imagen) {
             $image = new Imagen();
             $rnd = rand(0, 99999999);
             $path = Yii::$app->basePath . '/web/images';
             if (!file_exists($path)) {
                 mkdir($path, 0777);
             }
             $path = $path . '/Sucursal';
             if (!file_exists($path)) {
                 mkdir($path, 0777);
             }
             $path = $path . '/' . $model->id;
             if (!file_exists($path)) {
                 mkdir($path, 0777);
             }
             $imagen->saveAs($path . '/' . $rnd . '.' . $imagen->extension);
             $image->ur_imagen = $rnd . '.' . $imagen->extension;
             $image->save();
         }
         if ($saved) {
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
All Usage Examples Of yii\web\UploadedFile::getInstances