【问题标题】:Larverl's response()->file() must be prefixed with the current directoryLarverl 的 response()->file() 必须以当前目录为前缀
【发布时间】:2021-06-22 21:27:25
【问题描述】:

在全新的 Laravel 8 安装中,我遵循以下步骤:

  1. php artisan storage:link

  2. /public/storage/ 中创建文件夹images/ 并在其中粘贴一个名为picture.png 的图像

  3. 在 web.php 中我定义了这样的路由:

    Route::get('/picture', function(
       return response()->file(Storage::url("images/picture.png")); 
    ))
    

但是,如果我在浏览器中访问该路由,则图片不显示并抛出以下错误:

Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
The file "/storage/images/picture.png" does not exist

只有当response()->file()中的url以./为前缀表示当前目录时才有效,如下所示:

return response()->file('./'.Storage::url("images/picture.png")); 

我认为在本地使用相对路径不是一个好主意,存储和文件方法应该处理这个问题。也许这甚至是框架的问题。

任何想法如何绕过这个黑客?

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    我认为您没有配置本地驱动程序检查文档https://laravel.com/docs/8.x/filesystem#the-public-disk 并查找本地驱动程序。 如果您正确配置它,它应该返回您手动添加的“/”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 2021-11-25
      • 2020-03-15
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多