【问题标题】:How to upload files in web folder in yii2 advanced template?如何在yii2高级模板中上传web文件夹中的文件?
【发布时间】:2016-01-15 12:36:28
【问题描述】:

我尝试在后端上传文件,每次上传文件都成功上传并成功保存在数据库中,但它没有保存到我指定的目录,所以我的应用程序找不到文件,我已经为 web 目录中的上传文件夹授予 777 权限。以下是我的代码

模型处理和保存文件上传...

yii2高级模板如何上传根目录下的文件?

负责上传的模型

<?php

namespace backend\models;
use yii\base\Model;
use yii\web\UploadedFile;
use yii\Validators\FileValidator;


class UploadForm extends Model {

    public $img;
    public $randomCharacter;


    public function rules(){
        return[
             [['img'], 'file', 'skipOnEmpty' => false, 'extensions'=> 'png, jpg,jpeg'],
        ];
    }
    public function upload(){
        $path = '/uploads/';
        $randomString = '';
        $length = 10;
          $character = "QWERTYUIOPLKJHGFDSAZXCVBNMlkjhgfdsaqwertpoiuyzxcvbnm1098723456";
            $randomString = substr(str_shuffle($character),0,$length);
              $this->randomCharacter =  $randomString;
          if ($this->validate()){
            $this->img->saveAs($path .$randomString .'.'.$this->img->extension);
            return true;
        }else{
            return false;
        }

    }

}

负责将信息保存到数据库的产品型号

<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\web\UploadedFile;
use yii\Validators\FileValidator;


class Products extends \yii\db\ActiveRecord
{



    public static function tableName()
    {
        return 'products';
    }


    public function rules()
    {
        return [

            [['name'], 'string', 'max' => 100],
            [['descr', 'img', 'reviews'], 'string', 'max' => 255],
           // [['file'], 'file', 'skipOnEmpty' => false, 'extensions'=> 'png, jpg,jpeg']

        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [

            'img' => 'Img',

        ];
    }




}

my controller and the action
 public function actionCreate()
    {

      $time = time();

        $model = new Products();
        $upload = new UploadForm();

          if ($model->load(Yii::$app->request->post()) ) {
            //get instance of the uploaded file
               $model->img = UploadedFile::getInstance($model, 'img');
                 //define the file path
                    $upload->upload();
                     //save the path in the db
                        $model->img = 'uploads/' .$upload->randomCharacter .'.'.$model->img->extension;
                           $model->addtime = $time;
                               $model->save();
                     return $this->redirect(['view', 'product_id' => $model->product_id]); 

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

            }



        }

and my view file has been modified too thanks for any help

【问题讨论】:

    标签: yii2


    【解决方案1】:
    $path = '/uploads/';
    

    这意味着您正在将文件上传到系统中的顶级文件夹/uploads

    在您的终端中运行cd /uploads 并检查,最有可能的文件已上传到那里。

    要解决这个问题,您需要指定正确的完整路径。推荐的方法是使用别名。

    如果是高级应用,你已经有@backend别名,所以你可以使用它:

    $this->img->saveAs(\Yii::getAlias("@backend/web/uploads/{$randomString}.{$this->img->extension}");
    

    或者在common/config/bootstrap.php中创建自己的别名:

    Yii::setAlias('uploads', dirname(dirname(__DIR__)) . '/backend/web/uploads');
    

    然后像这样使用它:

    Yii::getAlias('@uploads');
    

    如果您不在根命名空间中,请不要忘记包含use Yii 或使用\Yii

    如果您希望您的图片可以在前端访问,您可以在前端和后端图片文件夹之间创建符号链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      相关资源
      最近更新 更多