multiple file not uploaded using yii2,data are not saved into database.It showing this error htmlspecialchars() expects parameter 1 to be string, array given.
Myform:
echo $form->field($model, 'product_img[]')->fileInput(['multiple' => true]);
Model:
{
return [
[['product_img'],'file', 'maxFiles' => 2],
];
}
Controller :
public function actionCreate()
{
$model = new Product();
if ($model->load(Yii::$app->request->post()) ) {
$model->file = UploadedFile::getInstances($model, 'product_img');
foreach ($model->file as $file) {
$model2 = new Product();
$model2->load(Yii::$app->request->post());
$model2->product_img='uploads/' . $file;
$sql = 'INSERT INTO `product`(`p_id`, `category`, `sub_category`, `product_img`, `product_name`) VALUES (Null,"'.($model2->category).'","'.($model2->sub_category).'","'.($model2->product_img).'","'.($model2->product_name).'")';
$command = \Yii::$app->db->createCommand($sql);
$command->execute();
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
}
return $this->render('view', [
'model' => $model,
]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}