При то что там где одна картинка все работает.
PHP Notice 'yii\base\ErrorException' with message 'Trying to get property 'extension' of non-object'
Ругаеться на ето
Код: Выделить всё
$modelProductImage->filename = strtotime('now') . '_' . \Yii::$app->getSecurity()->generateRandomString(6) . '.'.$addImgFile->extension;
Код: Выделить всё
public function actionMultipleImg()
{
$this->enableCsrfValidation = false;
if (\Yii::$app->request->isPost) {
$post = \Yii::$app->request->post();
$dir = \Yii::getAlias('@productImgPath') . '/additional-image/';
$result_link = str_replace('administrator', '', Url::home(true)) . 'storage/additional-image/';
$addImgFile = UploadedFile::getInstanceByName('ProductImage[attachment]');
$modelProductImage = new ProductImage();
$modelProductImage->filename = strtotime('now') . '_' . \Yii::$app->getSecurity()->generateRandomString(6) . '.'.$addImgFile->extension;
$modelProductImage->load($post);
$modelProductImage->validate();
if ($modelProductImage->hasErrors()) {
$result = ['error' => $modelProductImage->getFirstError('addImgFile')];
} else {
if ($addImgFile->saveAs($dir . $modelProductImage->filename)) {
$imag = \Yii::$app->image->load($dir . $modelProductImage->filename);
$imag->save($dir . $modelProductImage->filename, 90);
$result = ['filelink' => $result_link . $modelProductImage->filename, 'filename' => $modelProductImage->filename];
} else {
$result = ['error' => 'Ошибка'];
}//else
}//else
$modelProductImage->save();
\Yii::$app->response->format = Response::FORMAT_JSON;
return $result;
} else {
throw new BadRequestHttpException('Only POST is allowed');
}
}//action multiple img
Код: Выделить всё
<?php echo FileInput::widget([
'name' => 'ProductImage[attachment]',
'options' => ['accept' => 'image/*','multiple' => true],
'pluginOptions' => [
'deleteUrl' => Url::toRoute(['/product/delete-image']),
'initialPreview' => $model->imagesLinks,
'initialPreviewAsData'=>true,
'overwriteInitial' => false,
'initialPreviewConfig' => $model->imagesLinksData,
'uploadUrl' => Url::to(['/product/multiple-img']),
'uploadExtraData' => [
'ProductImage[product_id]' => $model->id,
],
'maxFileCount' => 10
],
'pluginEvents' => [
'filesorted' => new JsExpression('function(event, params){
$.post("' . Url::toRoute(["/product/sort-image", "id"=>$model->id]) . '", {position:params});
}')
],
]);?>