【问题标题】:Get the right URL to storage path (Laravel)获取存储路径的正确 URL (Laravel)
【发布时间】:2021-06-07 03:45:48
【问题描述】:

我正在尝试获取我在存储文件夹中托管的文件的完整 url,但我无法正确获取它。我使用 Laravel 7。

我要获取的网址是:

https://www.example.com/panel1/storage/app/folder/file.txt

我尝试了许多不同的方法,但始终找不到正确的地址。例如:

$url = asset('storage/app/folder/file.txt');
https://www.example.com/panel1/public/storage/app/folder/file.txt

$url = storage_path('app/folder/file.txt');
https://www.example.com/var/www/panel1/storage/app/folder/file.txt

$url = Storage::disk('local')->path('folder');
https://www.example.com/var/www/panel1/storage/app/storage/app/folder/file.txt

$url = Storage::url('app/folder/file.txt');
https://www.example.com/storage/app/folder/file.txt

我已经阅读了有关创建从存储文件夹到公用文件夹的符号链接的选项,但我担心这些文件可供任何用户访问,因为它们是私有的,并且它们的内容只能由所有者访问文件。

有谁知道我做错了什么以及如何纠正它?

提前致谢。

【问题讨论】:

    标签: php laravel path symlink laravel-7


    【解决方案1】:

    如果文件是私有的,那么您不能直接为任何人创建指向它们的 URL。如果您不想公开访问它们,那么您将不得不以某种方式代理它们:

    考虑这个示例代码:

    Route::get('privateFile/{filepath}', function ($filepath) {
        // Check if user can access file somehow
        return response->download(Storage::disk('local')->path('folder'));
    })->where('filepath', '.*')->middleware('auth')->name('privateFileDownload');
    
    

    然后生成您要执行的文件的 URL:

    route('privateFileDownload', [ 'filepath' => 'app/folder/file.txt' ]);
    

    这样你就可以控制谁可以下载哪些文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 2019-03-17
      • 2019-11-07
      • 2018-08-14
      • 2020-04-23
      • 1970-01-01
      相关资源
      最近更新 更多