我尝试使用Yii2文件上传上传文件,文件路径成功保存到数据库但文件未保存到我指定的目录..下面是我的代码..
<?php
namespace backend\models;
use yii\base\Model;
use yii\web\UploadedFile;
use yii\Validators\FileValidator;
use Yii;
class UploadForm extends Model
{
/**
* @var UploadedFile
*/
public $image;
public $randomCharacter;
public function rules(){
return[
[['image'], 'file', 'skipOnEmpty' => false, 'extensions'=> 'png, jpg,jpeg'],
];
}
public function upload(){
$path = \Yii::getAlias("@backend/web/uploads/");
$randomString = "";
$length = 10;
$character = "QWERTYUIOPLKJHGFDSAZXCVBNMlkjhgfdsaqwertpoiuyzxcvbnm1098723456";
$randomString = substr(str_shuffle($character),0,$length);
$this->randomCharacter = $randomString;
if ($this->validate()){
$this->image->saveAs($path .$this->randomCharacter .'.'.$this->image->extension);
//$this->image->saveAs(\Yii::getAlias("@backend/web/uploads/{$randomString}.{$this->image->extension}"));
return true;
}else{
return false;
}
}
}
用于创建fileupload的控制器
namespace backend\controllers;
use Yii;
use backend\models\Product;
use backend\models\ProductSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\UploadForm;
use yii\web\UploadedFile;
public function actionCreate()
{
$addd_at = time();
$model = new Product();
$upload = new UploadForm();
if($model->load(Yii::$app->request->post())){
//get instance of the uploaded file
$model->image = UploadedFile::getInstance($model, 'image');
$upload->upload();
$model->added_at = $addd_at;
$model->image = 'uploads/' .$upload->randomCharacter .'.'.$model->image->extension;
$model->save();
return $this->redirect(['view', 'product_id' => $model->product_id]);
} else{
return $this->render('create', [
'model' => $model,
]);
}
}
答案 0 :(得分:0)
是否会抛出任何错误?
这可能是许可问题。尝试将“uploads”目录权限更改为777(仅供测试)。
答案 1 :(得分:0)
使用表单数据加载产品($ model)。
if($model->load(Yii::$app->request->post()))
但是上传形式($ upload)永远不会填写你的脚本。因此,$ upload-&gt;图片将为空。
因为您声明'skipOnEmpty'=&gt;如果在UploadForm规则的文件验证器中为false,则$ upload上的验证将失败。
这就是为什么上述评论中的if语句(if($ upload-&gt; upload())不能保存$ model数据。
我不明白为什么你需要另一个模型来达到这个目的。它只会使事情变得复杂,所以我假设它是因为你从教程中复制了它。要修复并使事情变得更简单,只需执行以下操作:
将属性添加到产品型号
public $image;
将图像规则添加到产品型号
[['image'], 'file', 'skipOnEmpty' => false, 'extensions'=> 'png, jpg,jpeg'],
调整控制器创建操作
public function actionCreate()
{
$model = new Product();
if($model->load(Yii::$app->request->post()) && $model->validate()) {
// load image
$image = UploadedFile::getInstance($model, 'image');
// generate random filename
$rand = Yii::$app->security->generateRandomString(10);
// define upload path
$path = 'uploads/' . $rand . '.' . $image->extension;
// store image to server
$image->saveAs('@webroot/' . $path);
$model->added_at = time();
$model->image = $path;
if($model->save()) {
return $this->redirect(['view', 'product_id' => $model->product_id]);
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
这样的事情应该可以解决问题。
答案 2 :(得分:0)
你的UploadForm类已经在Backend上了,所以在UploadForm类的函数上传时应该是这样的: 改变这一行:
$path = \Yii::getAlias("@backend/web/uploads/");
到此:
$path = \Yii::getAlias("uploads")."/";