如何从公共/图像存储符号链接获取路径?

时间:2019-07-11 12:26:51

标签: php laravel symlink

我已经创建了一个指向本地存储“(php artisan storage:link)”的符号链接,然后将图像保存在我的公共文件夹中

public function store(PostStoreRequest $request)
    {
        $validated = $request->validated();
        if($request->hasFile('postImage')) {
            $path = $request->file('postImage')->store('public/images');
            $validated['postImage'] = $path;
        }

        $post = auth()->user()->posts()->create($validated);
        $post->categories()->attach($request->category);
        return redirect()->route('articulos.index');
    }

例如,$ path包含public/images/IdC24xVkoFZiJFWtyKQ2T1xBVrS0GGU3d2Z4NXgP.jpeg,它会将图像存储在storage / app / public / images中。

然后在刀片中显示如下图像:

<img class="card-custom-img-post" src="{{ asset('storage/'.$post->postImage) }}">

它生成以下URL:http://localhost/project/public/storage/public/images/IdC24xVkoFZiJFWtyKQ2T1xBVrS0GGU3d2Z4NXgP.jpeg

但是正确的是: http://localhost/project/public/storage/images/IdC24xVkoFZiJFWtyKQ2T1xBVrS0GGU3d2Z4NXgP.jpeg

是否无需替换或获得任何类似内容即可获取正确的网址?

1 个答案:

答案 0 :(得分:0)

您有2种解决方案:

您不要在路径中保存“ public /”字符串(您的路径将是“ images / ...”)。

或一些解决方法:

<img class="card-custom-img-post" src="{{ asset('storage/' . str_replace("public/", "", $post->postImage)) }}">