小编use*_*842的帖子

在 Yii2 中自动创建文件夹并上传图片

我已经创建了..\frontend\web\uploads.

这是我CreatePropertiesControllers.php配置中的功能:

public function actionCreate()
    {
        $model = new Properties();

        $date = date('YmdHis');

        if ($model->load(Yii::$app->request->post())) {            
            $file = \yii\web\UploadedFile::getInstance($model, 'url_img');
            if (!empty($file))
                $model->url_img = $date.$file;

            if($model->save())
            {
             if (!empty($file))
              $file->saveAs( Yii::getAlias('@frontend') .'/web/uploads/'.$date.$file);

              return $this->redirect(['view', 'id' => $model->id]);
            }
            return $this->render('create', ['model' => $model]);
        } else {
            return $this->render('create', ['model' => $model]);
        }
    }
Run Code Online (Sandbox Code Playgroud)

它在uploads存在时起作用。但我想重定向到新的文件夹uploads作为uploads\img

if (!empty($file))
                  $file->saveAs( Yii::getAlias('@frontend') .'/web/uploads/img'.$date.$file);
Run Code Online (Sandbox Code Playgroud)

它显示错误,因为../web/uploads/img不存在。

我不知道解决这个问题。帮我!

yii2 yii2-advanced-app

2
推荐指数
1
解决办法
2963
查看次数

标签 统计

yii2 ×1

yii2-advanced-app ×1