获取/读取laravel 5.8存储要查看的非公用文件夹文件?

时间:2019-04-02 16:37:45

标签: php laravel laravel-5 laravel-storage

  

尝试从我的视图访问“ storage / app / folder1 / a.png”

enter image description here

public function viewStorageFiles()
{
    $fileFullPath = Storage::disk('local')->path('folder1/a.png');
    $fileUrl = Storage::disk('local')->url('app/folder1/a.png');
    $storage_path = storage_path('app/folder1/a.png');

    return view('email.fileDownload')->with([
        'fileFullPath' => $fileFullPath,
        'fileUrl' => $fileUrl,
        'storage_path' => $storage_path,
        ]);
}
  

在视图中:email.fileDownload

<div>
    <p>  asset($fileUrl) ==> {{asset($fileUrl)}}</p>
    <img src="{{asset($fileUrl)}}"/>
</div>
<div>
    <p>  url($fileUrl) ==> {{url($fileUrl)}}</p>
    <img src="{{url($fileUrl)}}"/>
</div>
<div>
    <p>  storage_path($fileUrl) ==> {{storage_path($fileUrl)}}</p>
    <img src="{{storage_path($fileUrl)}}"/>
</div>

结果是:enter image description here

2 个答案:

答案 0 :(得分:1)

转到config / filesystems添加此数组

'public_site' => [
            'driver' => 'local',
            'root' => public_path('storage'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ]

那么您的功能可能就是这样

public function viewStorageFiles()
{
    $fileFullPath = Storage::disk('public_site')->path('folder1/a.png');
    $fileUrl = Storage::disk('public_site')->url('app/folder1/a.png');
    $public_path = public_path('storage/app/folder1/a.png');

    return view('email.fileDownload')->with([
        'fileFullPath' => $fileFullPath,
        'fileUrl' => $fileUrl,
        'storage_path' => $public_path,
        ]);
}

答案 1 :(得分:0)

对此可能会有很多答案!

您可以使用以下命令create a symbolic link from "public/storage" to "storage/app/public"

php artisan storage:link

以上命令会将您的storage/app/public目录映射到public目录。

现在,让我们考虑一下您的user1.jpg目录中有user2.jpgstorage/app/public个文件,您可以通过以下方式访问它们:

http://your-domain.com/storage/user1.jpg
http://your-domain.com/storage/user2.jpg

*根据您的评论更新了我的答案:*

您可以从受某些中间件保护的路由返回文件响应!

例如-以下路线从storage/app/uploads目录返回文件响应,该目录不可公开访问:

Route::get('storage/{file}', function ($file) {
    $path = storage_path('app' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $file);
    return response()->file($path);
});

您仍然可以确保沿路线上方行驶,并在您的视图中使用它。.

希望对您有所帮助。