简体   繁体   English

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

[英]Get / Read laravel 5.8 Storage non public Folder files to View?

Try to access 'storage/app/folder1/a.png' from my view 尝试从我的视图访问“storage / app / folder1 / a.png”

在此输入图像描述

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,
        ]);
}

In view : email.fileDownload 在视图中: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>

the result is : 结果是: 在此输入图像描述

Go to config/filesystems add this array 转到config / filesystems添加此数组

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

then your function could be like this 那你的功能就像这样

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,
        ]);
}

There could be many answers to this! 这可能有很多答案!

You could create a symbolic link from "public/storage" to "storage/app/public" using the following command: 您可以使用以下命令create a symbolic link from "public/storage" to "storage/app/public"

php artisan storage:link

Above command will map your storage/app/public directory to public directory. 上面的命令会将您的storage/app/public目录映射到public目录。

Now let's consider you have user1.jpg and user2.jpg files in your storage/app/public directory, you can access them in the following way: 现在让我们考虑您的storage/app/public目录中有user1.jpguser2.jpg文件,您可以通过以下方式访问它们:

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

* Updated my answer based on your comment: * *根据您的评论更新了我的答案:*

You can return a file response from a route that is protected by some middleware! 您可以从受某些中间件保护的路由返回文件响应!

For example - following route returns file response from storage/app/uploads directory that is not accessible publicly: 例如 - 以下路由返回无法公开访问的storage/app/uploads目录中的文件响应:

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

You could secure above route in anyway and use it in your views.. 您无论如何都可以在上面的路线上保护并在您的视图中使用它。

I hope that helped.. 我希望有帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM