因此,我通过执行iteasychannel跟随了YouTube上的Yii2教程,但是单击提交按钮时出现以下错误。
Call to a member function saveAs() on a non-object
以下是我创建表单并添加enctype的控制器操作中的一些代码,但仍然给出错误。
以下代码应将文件保存到目录,然后将文件的路径添加到表的化身列中。
$userprofile = UserProfile::findOne(['user_id' => $id]);
$imageName = $user->username;
$userprofile->file = UploadedFile::getInstance($userprofile, 'file');
$userprofile->file->saveAs('uploads/'.$imageName.'.'.$userprofile->file->extension);
$userprofile->avatar = 'uploads/'.$imageName.'.'.$userprofile->file->extension;
$userprofile->user_id = Yii::$app->user->id;
$userprofile->save(false);
请file
在UserProfile
模型中声明一个属性(变量),然后在rules()
方法中分配其验证规则-参考
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "user". (for eg)
*
* ...
*/
class UserProfile extends \yii\db\ActiveRecord
{
// for file uploading
public $file;
public function rules()
{
return [
// ... other attribute validation rules
[['file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
];
}
/**
* @inheritdoc
*/
public static function tableName()
{
return 'user'; // for eg
}
// ... rest of the code
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句