Here is the presentation:

<?php use yii\helpers\Html; use yii\widgets\ActiveForm; ?> <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> <?= $form->field($model, 'files')->fileInput(); ?> <div class="form-group"> <?= Html::submitButton( Yii::t('app','CREATE'), [ 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?> 

Here is the model:

 class Parser extends Model { public $files; public function rules() { return [ [['files'], 'file', 'skipOnEmpty' => true, 'extensions' => 'xml, yml'] ]; } } 

Here is the controller:

 $ execelparser = new Parser ();

         if ($ execelparser-> load (Yii :: $ app-> request-> post ())) {
             $ parsers = Yii :: $ app-> request;
             $ execelparser-> files = UploadedFile :: getInstance ($ execelparser, 'files');
              return var_dump ($ execelparser-> files);
         } else {
             return $ this-> render ('parser', [
                 'model' => $ execelparser
             ]);
         }

I don't get here at all:

 if ($execelparser->load(Yii::$app->request->post())){} 

because nothing comes. If you print it like this:

 return var_dump($execelparser->files); 

I get this answer:

  /var/www/magaz.lc/backend/controllers/GodsController.php:288: object(backend\models\Parser)[89] public 'files' => null private '_errors' (yii\base\Model) => null private '_validators' (yii\base\Model) => null private '_scenario' (yii\base\Model) => string 'default' (length=7) private '_events' (yii\base\Component) => array (size=0) empty private '_behaviors' (yii\base\Component) => null 

If so:

 var_dump(Yii::$app->request); 

here is the answer:

 var/www/magaz.lc/backend/controllers/GodsController.php:269: object(yii\web\Request)[8] public 'enableCsrfValidation' => boolean true public 'csrfParam' => string '_csrf-backend' (length=13) public 'csrfCookie' => array (size=1) 'httpOnly' => boolean true public 'enableCsrfCookie' => boolean true public 'enableCookieValidation' => boolean true public 'cookieValidationKey' => string 'a2vnMIxR40yJ_kbBhCdOEMBt-NkTl6TO' (length=32) public 'methodParam' => string '_method' (length=7) public 'parsers' => array (size=0) empty private '_cookies' => null private '_headers' => null private '_rawBody' => null private '_bodyParams' => null private '_queryParams' => null private '_hostInfo' => null private '_baseUrl' => string '/admin' (length=6) private '_scriptUrl' => string '/backend/web/index.php' (length=22) private '_scriptFile' => null private '_pathInfo' => string 'gods/parser' (length=11) private '_url' => string '/admin/gods/parser' (length=18) private '_port' => null private '_securePort' => null private '_contentTypes' => null private '_languages' => null private '_csrfToken' => null private '_isConsoleRequest' (yii\base\Request) => null private '_events' (yii\base\Component) => array (size=0) empty private '_behaviors' (yii\base\Component) => null private '_scriptFile' (yii\base\Request) => null 

explain to me, please, what am i doing wrong?

    1 answer 1

    You obviously lack something in the controller. Try to bring your code to this form:

     public function upload ()
     {
         $ execelparser = new Parser ();
         if ($ execelparser-> load (Yii :: $ app-> request-> post ()) && $ execelparser-> save ()) {
             $ execelparser-> files = UploadedFile :: getInstance ($ execelparser, 'files');
             if ($ execelparser-> files) {
                 $ execelparser-> files-> saveAs ('your / path /'. $ execelparser-> file-> baseName. '.'. $ execelparser-> file-> extension);
                 $ execelparser-> files1 = $ execelparser-> file-> baseName.  '.'  .  $ execelparser-> file-> extension;  // write the path to the database if necessary
         }
         return $ this-> render ('parser', [
             'model' => $ execelparser
         ]);
         }
     }