N
N
nepster-web2014-04-29 01:12:56
Yii
nepster-web, 2014-04-29 01:12:56

How to organize multiple file uploads in Yii2?

View

<?php $form = ActiveForm::begin([
                'options' => ['enctype'=>'multipart/form-data']
        ]); ?>
    
    <?= $form->field($model, 'image')->widget(\dosamigos\fileinput\BootstrapFileInput::className(), [
        'options' => ['accept' => 'image/*', 'multiple' => true],
        'clientOptions' => [
            'previewFileType' => 'text',
            'browseClass' => 'btn btn-success',
            'uploadClass' => 'btn btn-info',
            'removeClass' => 'btn btn-danger',
            'removeIcon' => '<i class="glyphicon glyphicon-trash"></i> '
        ]
    ]);?>
    
    <?php echo Html::submitButton('Update') ?>
    <?php ActiveForm::end(); ?>

controller
public function actionTest()
  {
        $dir = Yii::getAlias('@frontend/../web/uploads/test/');
        
        $uploaded = false;
        
        $model = new Test();
        
        
            echo '<pre>';
            print_r($_POST);
            echo '</pre>';
        if ($model->load($_POST)) {
            
            
            
            $file = UploadedFile::getInstance($model, 'image[]');
            
            echo '<pre>';
            print_r($file);
            echo '</pre>';
            
            die();
            
            /*
            $model->image = $file;
            if ($model->validate()) {

                $uploaded = $file->saveAs( $dir . $file->name );
            }*/
        }
        
        
        return $this->render('test',[
             'model' => $model,
             'uploaded' => $uploaded,
             'dir' => $dir,
           ]);
  }

Result:
Array
(
    [_csrf] => amwuNnIuaXNeKhlDNhYFITgtZ2E5STw4AgofRkN/DgUBDkpcLXw7Ng==
    [Test] => Array
        (
            [image] => 
        )

)

Tell me, please, why can't I get an array of these files?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
enchikiben, 2014-04-29
@nepster-web

$file = UploadedFile::getInstances($model, 'image');

I'll just leave it here https://github.com/yiisoft/yii2/blob/master/framew...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question