Hi all, i’m following this guide to try upload file funcition.
But when i press Submit, i have this error:
PHP Fatal Error – yii\base\ErrorException
Call to a member function saveAs() on a non-object
This is my controller
...
use app\models\admin\UploadForm;
use yii\web\UploadedFile;
$upform = new UploadForm();
if (Yii::$app->request->isPost) {
$upform->file = UploadedFile::getInstance($upform, 'file');
if ($upform->validate()) {
$upform->file->saveAs('uploads/' . $upform->file->baseName . '.' . $upform->file->extension);
}
}
...
this is the view:
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($upform, 'file')->fileInput() ?>
<button>Submit</button>
<?php ActiveForm::end(); ?>
and the simply model:
namespace app\models\admin;
use yii\base\Model;
use yii\web\UploadedFile;
/**
* UploadForm is the model behind the upload form.
*/
class UploadForm extends Model
{
/**
* @var UploadedFile|Null file attribute
*/
public $file;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['file'], 'file'],
];
}
}
I don’t know what i’m missing,
Thank’s for any help!