Yii Framework Forum: Call to a member function saveAs() on null - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Call to a member function saveAs() on null how to fix it?

#1 User is offline   bornfromash 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 12-October 17

Posted 12 October 2017 - 09:53 PM

Hello im new in here

i got stuck on my controller content upload

Quote

public function actionCreate()
{
$model = new Karyawan();

if ($model->load(Yii::$app->request->post()) && $model->save())
{

//get the instance of the upload file
$imageName = $model->nama_karyawan;
$uploadedFile=UploadedFile::getInstance($model,'uploadedFile');
$model->file->saveAs('images/'.$imageName.'.'.$model->file->extension );

// save the path in the db collumn
$model->foto = 'images/'.$imageName.'.'.$model->file->extension;
$model->save();

return $this->redirect(['view', 'id' => $model->id_karyawan]);

} else {
return $this->render('create', [
'model' => $model,
]);
}
}


Error message

Quote

$model->file->saveAs('images/'.$imageName.'.'.$model->file->extension );


please i need help
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,539
  • Joined: 12-October 09
  • Location:Croatia

Posted 13 October 2017 - 01:23 AM

UploadedFile::getInstance() can return "null" if there is no file uploaded so you need to check for that possibility.


btw. is this for Yii1.1.x (you posted here) or 2.0?
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   bornfromash 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 12-October 17

Posted 13 October 2017 - 02:11 AM

its still error, i already make sure if i choose the file and then error message is message is

Quote

Undefined variable: imageName


its yii2 advanced anyway.
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,539
  • Joined: 12-October 09
  • Location:Croatia

Posted 13 October 2017 - 02:19 AM

well, by your error message it does nothing to do with the uploaded file... but with the $imageName variable.. debug it to see why it's not set.

/* NOTE: moved to Yii2 section */
Find more about me.... btw. Do you know your WAN IP?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users