无法访问Laravel存储目录

时间:2018-12-13 18:19:04

标签: php laravel laravel-5 laravel-5.7 laravel-storage

我正在使用Laravel 5.7。在我的本地主机上,我的存储正常,并且所有图像也都显示,但是在我的主机上,新图像不显示。

我在blade.php文件中使用以下代码:

{{ asset("storage/$slider") }}

在我的本地主机上,它显示图像:

localhost:8000/storage/sliders/HKeGwcvnXbjuiA6g9wsjnoqphJc5DGup78D92b4F.jpeg

在我的网站上,我无法访问:

http://mywebsite.com/storage/sliders/HKeGwcvnXbjuiA6g9wsjnoqphJc5DGup78D92b4F.jpeg

1 个答案:

答案 0 :(得分:1)

现在我使用这条路线并且有效:

Route::get('storage/sliders/{filename}', function ($filename)
{

    $path = storage_path('app/public/sliders/' . $filename);

    if (!File::exists($path)) {
        abort(404);
    }

    $file = File::get($path);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;
});

如果您还有其他子目录,可以使用:

Route::get('storage/{path}/{filename}', function ($path,$filename)
{

    $path = storage_path('app/public/'.$path.'/' . $filename);

    if (!File::exists($path)) {
        abort(404);
    }

    $file = File::get($path);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;
});
相关问题