laravel 如何将laravel 5.3中的存储路径更改为公共?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39154484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:21:57  来源:igfitidea点击:

How to change storage path in laravel 5.3 to public?

laravellaravel-5laravel-5.3

提问by MilMike

In laravel 5.3, if I upload a file from a form, it will be stored in own storage directory. Then I should create a symlinkto that storage path in public path.

在 中laravel 5.3,如果我从表单上传文件,它将存储在自己的存储目录中。然后我应该symlink在公共路径中创建一个到那个存储路径。

I can't use creating symlinks on my system because of the limitation. How can I upload files directly to public/uploadsinstead of storage/app/uploads?

由于限制,我无法在我的系统上使用创建符号链接。如何将文件直接上传到public/uploads而不是storage/app/uploads

I tried to set the path in AppServiceProvider.phpbut it doesn't work.

我试图设置路径,AppServiceProvider.php但它不起作用。

public function register()
{
    //not working:
    $this->app->useStoragePath( $this->app->basePath() .  "/upload");

    //also not working:
    // $this->app->bind('path.storage', function () {
    // return $this->app->basePath() . '/upload';
    // });

}

by "not working" I mean that it is still uploading into the default storage directory. I cleared also the cache.

“不工作”是指它仍在上传到默认存储目录。我也清除了缓存。

Here is the upload part (used in a controller):

这是上传部分(在控制器中使用):

        $request->image->storeAs("uploads", "uploaded_image.jpg");

Thanks for any hints. :)

感谢您的任何提示。:)

回答by Geoherna

In Laravel 5 you now have to do this in config/filesystems.php. You can add a new disk like so:

在 Laravel 5 中,你现在必须在config/filesystems.php. 您可以像这样添加新磁盘:

'disks' => [

    'uploads' => [
          'driver' => 'local',
          'root'   => public_path(), // previously storage_path();
    ],

]

and then do the following to use it:

然后执行以下操作来使用它:

Storage::disk('uploads')->put('filename', $file_content);