带有流明的file_get_contents

时间:2015-12-10 00:03:17

标签: php laravel file-get-contents lumen

我把这段代码放到一个函数(php类)中:

$theFile = '/test/test.xml'; // these are in the public folder
dd(file_get_contents($theFile));

如果我转到mydomain.local/test/test.xml,我会得到正常工作的xml代码。

但是对于file_get_contents,我收到此错误:

file_get_contents(/test/test.xml): failed to open stream: No such file or directory

如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您正在传递相对于服务器基本目录的函数的绝对路径。这不是URL的相同基目录。尝试传递相对于当前执行脚本的路径。

答案 1 :(得分:1)

Lumen没有你在Laravel中熟悉的public_path()来轻松获取公共文件的路径。

重新实现它的最简单方法是向项目中添加一个名为irazasyed/larasupport的包,它会添加各种缺少的帮助程序(包括public_path())以及添加缺少的vendor publish命令来自Lumen。

或者,如果您不想添加第三方软件包,只需在您的app目录中创建一个名为helpers.php的文件,然后在composer.json文件中在“autoload”部分中添加以下内容并运行composer dump-autoload刷新自动加载器缓存:

"files": [
    "app/helpers.php"
],

然后在helpers.php内添加以下内容:

<?php
if (!function_exists('public_path')) {
   /**
    * Get the path to the public folder.
    *
    * @param  string $path
    * @return string
    */
    function public_path($path = '')
    {
        return env('PUBLIC_PATH', base_path('public')) . ($path ? '/' . $path : $path);
    }
}