In my application i have a code like:
$model->imageFile = UploadedFile::getInstanceByName('imageFile')
I’m 100% sure it worked some time ago but it doesn’t anymore.
$model->imageFile
gives always null but it should be an object.
Has something changed in UploadedFile class? Can anyone see what is wrong?
Thank you.
My controller
public function actionUpload()
{
$uploadPath = Yii::getAlias('@frontend') . '/web/uploads/images';
$model = new UploadForm();
$model->setPath($uploadPath);
if (Yii::$app->request->isPost) {
$model->imageFile = UploadedFile::getInstanceByName('imageFile');
if ($model->upload()) {
// file is uploaded successfully
return $this->redirect('/image');
}
}
echo json_encode(['error' => $model->errors['imageFile'][0]]);
exit;
}
View
use kartik\file\FileInput;
<?= Html::label('Dodaj zdjęcia') ?>
<?= FileInput::widget([
'name' => 'imageFile',
'options' => ['multiple' => true],
'pluginOptions' => [
'showPreview' => false,
'uploadUrl' => Url::to(['image/upload']),
'maxFileCount' => 10
]
]); ?>
UploadForm.php
public function upload()
{
$filename = $this->imageFile->baseName; // here is the problem: PHP Notice 'yii\base\ErrorException' with message 'Trying to get property 'baseName' of non-object'
if(!file_exists($this->uploadPath)){
mkdir($this->uploadPath, 0777, true);
}
if ($this->validate()) {
$this->imageFile->saveAs($this->uploadPath . '/' . $filename . '.' . $this->imageFile->extension);
return true;
} else {
return false;
}
}