Я хочу загрузить изображение и сохранить его в своей базе данных. Здесь имя поля в базе данных image_path
. Когда я пытаюсь загрузить свое изображение, оно показывает ошибку: вызов функции-члена saveAs()
для необъекта в строке
$customer->file->saveAs('uploads/customer/' . $customer->file->baseName . '.' . $customer->file->extension);
Если я напечатаю var_dump($customer->file);
, он вернет NULL.
Может ли кто-нибудь помочь мне решить эту проблему.
Это мое мнение:
<?php $form = ActiveForm::begin([
'id' => 'my-profile',
'action' => \Yii::$app->urlManager->createUrl(['/myprofile', 'id_customer' => Yii::$app->user->identity->id_customer]),
'options' => ['enctype'=>'multipart/form-data']
]); ?>
<?= $form->field($customer, 'file')->fileInput() ?>
<?php ActiveForm::end(); ?>
Это моя модель:
public $file;
public function rules()
{
return [
['active', 'default', 'value' => self::STATUS_ACTIVE],
['active', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
[['file'],'file'],
[['name', 'image_path'], 'string', 'max' => 200],
];
}
public function attributeLabels()
{
return [
'file' => 'Profile Picture ',
];
}
Это мой контроллер:
public function actionMyprofile(){
$customer->file = UploadedFile::getInstance($customer,'image_path');
$customer->file->saveAs('uploads/customer/' . $customer->file->baseName . '.' . $customer->file->extension);
$customer->file = 'uploads/customer/' . $customer->file->baseName . '.' . $customer->file->extension;
}
image_path
наfile
в контроллере, см. правила вашей модели. - person Insane Skull   schedule 14.12.2015UploadedFile::getInstance($customer,'image_path');
должно бытьUploadedFile::getInstance($customer,'file');
, так как<?= $form->field($customer, 'file')->fileInput() ?>
- person ineersa   schedule 14.12.2015var_dump(ini_get('file_uploads'))
,var_dump($_FILES)
- person SilverFire   schedule 14.12.2015