【问题标题】:laravel storage exists returns false although file exists尽管文件存在,laravel storage exists 返回 false
【发布时间】:2021-07-03 14:37:12
【问题描述】:

我正在 laravel 中使用 Fpdi 修改上传的 pdf。 pdf 生成完美,我可以在任何文件资源管理器中看到它。但是 Laravel 内置 Storage::exists() 返回 false。 这是一个非常奇怪的错误,因为 php file_exists 方法可以完美地找到具有相同路径的文件,以及 File::exists

我完全没有想法。任何帮助将不胜感激!

src 变量由 Storage::path() 生成

$src = Storage::path("notes/".$this->note->id."-downgraded.pdf");
Storage::disk('public')->exists($src); //this returns false
file_exists($src); //this returns true
File::exists($src) //this returns true

【问题讨论】:

  • 你试过File::exists($src)
  • @HassaanAli 感谢您的想法,File:exists 也返回 true!
  • 请纠正我,您只想使用Storage::disk 处理文件?
  • 你能提供一些关于$src的额外信息吗?另外请查看公共光盘文档:laravel.com/docs/8.x/filesystem#the-public-disk。如果你使用磁盘,这个磁盘通常会配置一个基本路径,因此$src应该是相对于这个路径的。
  • 使用存储盘功能无法访问公共路由。它用于访问 laravel 应用程序内部的私有存储。为了访问公共目录 File::exists 用于访问它。 :)

标签: php laravel file storage exists


【解决方案1】:

我假设你的 laravel 项目正在使用默认的 laravel 文件系统配置。

如果您使用Storage:disk('public'),则此磁盘上的根目录不同于Storage:path()
这意味着您实际上已经从一个磁盘检索了$src,并检查了它是否存在于一个完全不同的磁盘上。

如果您在没有磁盘的情况下使用Storage,则将使用默认磁盘。默认为local,因此 $src 是这样的:/my-project/storage/app/notes/some.pdf
(请参阅./config/filesystem.php 以检查您的实际磁盘和默认配置)

您这样检查存在性:Storage::disk('public')->exists($src)
这意味着您在公共磁盘上查找/my-project/storage/app/notes/some.pdf
公共磁盘的根目录一般为/my-project/storage/app/public

这意味着exists-method实际上是在检查是否存在 /my-project/storage/app/public/my-project/storage/app/notes/some.pdf。 我认为这不是您所期望的。

您的存在检查应如下所示:

# public disk root-directory is configured this: 
# /my-project/storage/app/public

# file is actually here: 
# /my-project/storage/app/public/notes/some.pdf

$src = "notes/some.pdf"
Storage::disk('public')->exists($src);

使用Storage 根本不需要您知道文件的绝对路径。使用得当,你甚至根本不需要关心文件的位置。

我强烈建议在这里阅读手册:https://laravel.com/docs/master/filesystem

【讨论】:

    【解决方案2】:

    感谢 Hassaan Ali 的评论,我设法使用 File 而不是 Storage 使其工作

    【讨论】:

      猜你喜欢
      • 2017-01-03
      • 1970-01-01
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多