文件未在yii2中上传 [英] File Not uploading in yii2
本文介绍了文件未在yii2中上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想上传图像并将其保存到数据库中.
在此,数据库中的字段名称为image_path
.尝试上传图片时,它显示错误:在行
I want to upload an image and save it into my database.
Here, the field name in database is image_path
. When, I am trying to upload my image it shows an error: Call to a member function saveAs()
on a non-object on line
$customer->file->saveAs('uploads/customer/' . $customer->file->baseName . '.' . $customer->file->extension);
如果我打印var_dump($customer->file);
,它将返回 NULL .
If I print var_dump($customer->file);
it returns NULL.
任何人都可以帮助我解决此问题.
Can anyone help me to resolve this issue.
这是我的观点:
<?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;
}
推荐答案
查看:
<?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, 'image_path')->fileInput() ?>
<?php ActiveForm::end(); ?>
型号:
public function rules()
{
return [
['active', 'default', 'value' => self::STATUS_ACTIVE],
['active', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
[['image_path'],'file'],
[['name', 'image_path'], 'string', 'max' => 200],
];
}
public function attributeLabels()
{
return [
'image_path' => 'Profile Picture ',
];
}
控制器:
public function actionCreate()
{
$model = new YourModel_name();
if ($model->load(Yii::$app->request->post())) {
$model->image_path = UploadedFile::getInstance($model, 'image_path');
$filename = pathinfo($model->image_path , PATHINFO_FILENAME);
$ext = pathinfo($model->image_path , PATHINFO_EXTENSION);
$newFname = $filename.'.'.$ext;
$path=Yii::getAlias('@webroot').'/image/event-picture/';
if(!empty($newFname)){
$model->image_path->saveAs($path.$newFname);
$model->image_path = $newFname;
if($model->save()){
return $this->redirect(['your redirect_path', 'id' => $model->id]);
}
}
}
return $this->render('create', [
'model' => $model,
]);
}
这篇关于文件未在yii2中上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文