Laravel 从工作和路线中打开

Laravel fopen from job and from route

当我 运行 来自 /test 路线的这段代码时,它运行良好。但是当我从工作或命令中 运行 它失败时(fopen 找不到文件)。 运行这两种方法有什么区别?

$path = 'tempdir1/tempdir2' /* works from the `/test` route, fails from the job class */
fopen($path, 'r');

看来问题出在路径上。这就是为什么您应该始终使用绝对路径,例如:

public_path('test/tempdir/tempdir2');

因为您永远不知道您的代码将来自哪个目录运行。

在Laravel中有很多这样的助手,例如base_path()storage_path()