Answer the question
In order to leave comments, you need to log in
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(); ?>
class Parser extends Model
{
public $files;
public function rules()
{
return [
[['files'], 'file', 'skipOnEmpty' => true, 'extensions' => 'xml, yml']
];
}
}
$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
]);
}
if ($execelparser->load(Yii::$app->request->post())){}
return var_dump($execelparser->files);
/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
var_dump(Yii::$app->request);
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
Answer the question
In order to leave comments, you need to log in
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 theif (Yii::$app->request->isPost)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question