如何在laravel存储目录中保存图像

问题描述

我需要将使用 base64 转换的图像保存到存储目录中,而不是将其保存在 public 文件夹中,

这是我的商店功能,它将图像保存到我的 public 目录中,

public function store(Request $request,Category $category)
{
    if ($request->image) {
        $name = time().'.'. explode('/',explode(':',substr($request->image,strpos($request->image,';')))[1])[1];

        Image::make($request->image)->save(public_path('img/category/').$name);

    return response('success');
}

我需要知道如何将图像保存到我的存储文件夹中,

解决方法

Laravel 8 official documentation 中所述,在我的本地 Laravel 安装中,我导入了以下内容:

use Illuminate\Support\Facades\Storage;

然后在 store 方法中:

Storage::disk('local')->put(time() . '.' . 'example.txt','Contents');

它将文件存储到 storage/app 而不是公共目录。