HI, while I'm trying to upload multiples files I got this error, I followed this guide:" http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html " I got exactly the same thing but still the same error, even when I have " = Html::csrfMetaTags() ?>" in the main layout
I disabled csrf validation but nothing happened. in the controller I have:
public function actionUpload() {
$model = new UploadForm();
if (Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstances($model, 'file');
if ($model->file && $model->validate()) {
foreach ($model->file as $file) {
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
}
}
}
return $this->render('upload', ['model' => $model]);
}
//==================in The View================================
$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);
?>
= $form->field($model, 'file[]')->fileInput(['multiple' => true]) ?>
<button>Submit</button>
//===================in The Model=========================
class UploadForm extends Model
{
/**
* @var UploadedFile|Null file attribute
*/
public $file;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['file'], 'file', 'maxFiles' => 10], // <--- here!
];
}
}
I have just encountered this issue too.
But this seems not to be related to the max files, but more multiple file upload
testing scenario
1) Uploading 1 File works fine, POST data includes _csrf and form model data. body includes required file data.
2) multiple files upload from the same form fails with 400 error, Post data missing _csrf token AND model data. Body does contain multipart file data including _csrf token but it is different to the session _csrf token.
Used Yii debugger to find this info.
Hope this helps find the issue
As a workaround limit uploads to 1 at a time ;(
exactly, I got the same too, single uploads works fine, but not when I changed the code to work with mutiples files
i have this issue after uploading single file >8Mb.
You all should check your php and web server configs. Make sure your limits are correct.
php options: post_max_size, upload_max_filesize, max_input_time
web server: nginx - client_max_body_size , apache - LimitRequestBody
if you dont exceed the limits, you should not receive the bad request.
The same issue was happening for me when i upload one file. the issue was related to file size. i changed the settings as per @dmitriy-krista suggestion and it worked. thanks
Yep, seems to be issue with trimming POST. Can't be solved by framework.
This issue is due to post_max_size = 8M in php.ini file.You can increase it to solve the issue
Most helpful comment
You all should check your php and web server configs. Make sure your limits are correct.
php options: post_max_size, upload_max_filesize, max_input_time
web server: nginx - client_max_body_size , apache - LimitRequestBody
if you dont exceed the limits, you should not receive the bad request.