php - 获取/读取 laravel 5.8 存储非公共(public)文件夹文件以查看?

标签 php laravel laravel-5 laravel-storage

Try to access 'storage/app/folder1/a.png' from my view

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

In view : 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

最佳答案

这个问题可能有很多答案!

您可以使用以下命令创建从“public/storage”到“storage/app/public”的符号链接(symbolic link):

php artisan storage:link

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

现在假设您的 storage/app/public 目录中有 user1.jpguser2.jpg 文件,您可以访问它们通过以下方式:

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

您可以无论如何保护上述路线并在您的 View 中使用它..

我希望有所帮助..

关于php - 获取/读取 laravel 5.8 存储非公共(public)文件夹文件以查看?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55479687/

相关文章:

eloquent - Laravel eloquent 使用 with() 方法连接多个表并搜索数据

php - 在 Laravel 中为不同客户模式创建关系数据库模型的最佳方法

php - 我正在尝试扩展 CodeIgniter 表单验证库

phpmyadmin。自动登录

PHP 不会向 mysql 发送数据!? ( undefined variable 错误)

php - laravel4 迁移失败(pdo 异常)

php - 将 php 代码链接到 html 页面

PHP/MySQL - 不允许我插入(或替换)右单引号 "’ "

php - 提交后选择要打开的选项卡。

php - 关系字段上的排序依据