내가 사용하고 yii2 - 위젯 fileinput 함수를 형태로 이미지 업로드하십시오. 업로드 또는 생성 버튼을 클릭하면 Trying to get property of non-object
컨트롤러에 오류가 발생합니다.
제어 장치
public function actionCreate()
{
Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/uploads/';
$model = new Ads();
$provinces = ArrayHelper::map(Province::find()->all(), 'name', 'name');
if ($model->load(Yii::$app->request->post())){
$image = UploadedFile::getInstances($model, 'image');
$model->filename = $image->name;
$ext = end((explode(".", $image->name)));
$avatar = Yii::$app->security->generateRandomString().".{$ext}";
$path = Yii::$app->params['uploadPath'].$avatar;
if ($model->save()) {
$image->saveAs($path);
$model->image_adr = $path;
return $this->redirect(['view', 'id' => $model->id]);
}else{
echo "error on saving the model";
}
}
return $this->render('create', [
'model' => $model,
'provinces'=>$provinces,
]);
}
모델 규칙
public function rules()
{
return [
[['type', 'explanation', 'cost', 'province_name', 'address'], 'required'],
[['type', 'explanation', 'image_adr', 'address'], 'string'],
[['cost'], 'integer'],
[['province_name'], 'string', 'max' => 20],
[['province_name'], 'exist', 'skipOnError' => true, 'targetClass' => Province::className(), 'targetAttribute' => ['province_name' => 'name']],
[['image'],'safe'],
[['image'], 'file', 'extensions'=>'jpg, gif, png', 'maxFiles'=>3,],
];
그리고 마지막으로 보기
<?= $form->field($model, 'image[]')->widget(FileInput::classname(), [
'options'=>['accept'=>'image/*', 'multiple'=>true],
'pluginOptions'=>['allowedFileExtensions'=>['jpg','gif','png'], 'overwriteInitial'=>false,]
]); ?>
문제는 컨트롤러 에서이 부분을 참조해야한다고 생각합니다.
$image = UploadedFile::getInstances($model, 'image');
오류 이미지가 도움이 될 수 있습니다.
문제는 사용할 때 배열로 UploadedFile::getInstances($model, 'image');
작업 foreach
하거나 처리 해야한다는 것입니다 .
나를 문제로 만든 것은 비록 당신이 사용하고 있더라도 UploadedFile::getInstanc
(결국 쓸모없는 s에 주목) 여전히 그것을 배열처럼 취급해야하고 모든 부분에서 외롭지 $image[0]
않고 사용해야한다는 것 $iamge
입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다