Bad Request (#400)
Unable to verify your data submission.
This is not Yii’s fault. This happens because you upload a file too large and the csrf token for yii simply does not have more room to pass, thus not getting validated at all. To fix this you should increase the max upload file size from your php.ini or to implement an ajax uploader that uploads in chunks.
but yii must check it and show error at the corresponding field, as show for single file upload
controller:
public function actionCreate()
{
$model = new Item();
$images = new Images();
if(
($_GET['category_id'] < 1) ||
!isset($_GET['category_id']) ||
!is_numeric($_GET['category_id'])
) throw new NotFoundHttpException('Category ID is not defined or acceptable!');
if ($model->load(Yii::$app->request->post()) ) {
if(Yii::$app->request->isPost){
$images->images = UploadedFile::getInstances($images, 'images');
if($model->validate() && $images->validate()){
foreach($images->images as $key => $value)
$images->images[$key]->saveAs('..\\..\\images\\' . $images->images[$key]->baseName . '.' . $images->images[$key]->extension);
$model->category_id = $_GET['category_id'];
$model->date = time();
if($model->save()) return $this->redirect(['view', 'id' => $model->id]);
else throw new NotFoundHttpException('Item can not be saved!');
}else return $this->render('create', ['model'=>$model, 'images'=>$images]);
}
} else return $this->render('create', ['model' => $model, 'images'=>$images, ]);
}
images model:
<?php
namespace common\models;
use yii\base\Model;
use yii\web\UploadedFile;
class Images extends Model
{
public $images = array();
public function rules(){
return [
[['images'], 'required'],
[['images'], 'file', 'extensions' => ['jpg', 'png'], 'maxFiles' => 20],
];
}
}
Yes, something like that. The issue is that you’re getting POST alright but it’s trimmed. Doensn’t contain all necessary values and we can’t really say if it wasn’t submitter or it was trimmed because of the limit.
Hi. This error is related to PHP not Yii. This is because of server configuration. You should increase both post_max_size and upload_max_filesize (post_max_size should be more than upload_max_filesize). The posted values (containing files) are trimmed as mentioned and there is no way for Yii to determine which file is trimmed since it hasn’t the original files to compare and each data (such as size, mime type, etc.) are extracted from the uploaded file itself. Actually event PHP itself never can tell where the data is trimmed exactly.
Even if CSRF token is successfully passed, still you don’t have the files successfully uploaded. You can test it via a simple PHP form (without framework) yourself. The hidden constant (MAX_FILE_SIZE) in the form should not be greater than upload_max_filesize in php.ini and as you can see in this link there is a specific error code for php.ini config beside the hidden form element (MAX_FILE_SIZE).