S
S
Sergey Beloventsev2016-10-29 09:43:55
Yii
Sergey Beloventsev, 2016-10-29 09:43:55

Why is an empty $_FILES coming?

here is the view

<?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 output 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);
then this 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 what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Fedorov, 2016-10-29
@Sergalas

Loading a file takes place in a slightly different way than passing the usual parameters. When you submit the form, the following happens:
1. A file is uploaded to the server
2. The UploadedFile::getInstance($execelparser, 'files') method first generates a file identifier based on the model and attribute name, then retrieves the uploaded file and returns
3. Accordingly $
execelparser- >files = UploadedFile::getInstance($execelparser, 'files') writes the received file to the model. until this code works for you, there will be nothing in the model, because the load(Yii::$app->request->post()) method cannot load the file into the model. In fact, you just need to replace
on the
if (Yii::$app->request->isPost)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question