且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 yii2 高级模板中上传 web 文件夹中的文件?

更新时间:2022-11-05 13:26:31

$path = '/uploads/';

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

That means you are uploading files to /uploads, the top level folder in the system.

在终端中运行 cd/uploads 并检查,很可能文件已上传到那里.

Run cd /uploads in your terminal and check, most likely files were uploaded there.

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

To fix that, you need to specify correct full path. The recommended way to do it is using aliases.

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

In case of advanced application, you already have @backend alias, so you can use it:

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

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

Or create your own alias in common/config/bootstrap.php:

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

然后像这样使用它:

Yii::getAlias('@uploads');

不要忘记包含 use Yii 如果你不在根命名空间或使用 \Yii;

Don't forget to include use Yii if you are not in root namespace or use \Yii;

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

If you want your images to be accessible on frontend, you can create symlink between frontend and backend images folders.