Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用流明获取文件内容_Php_Laravel_File Get Contents_Lumen - Fatal编程技术网

Php 使用流明获取文件内容

Php 使用流明获取文件内容,php,laravel,file-get-contents,lumen,Php,Laravel,File Get Contents,Lumen,我将此代码转换为函数(php类): 如果我转到mydomain.local/test/test.xml,我会得到工作的xml代码 但是使用文件\u获取\u内容,我得到以下错误: file_get_contents(/test/test.xml): failed to open stream: No such file or directory 如何解决此问题?您正在向函数传递一个相对于服务器基本目录的绝对路径。对于URL,不需要相同的基本目录。尝试传递相对于当前执行脚本的路径。Lumen没有您

我将此代码转换为函数(php类):

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


但是使用
文件\u获取\u内容
,我得到以下错误:

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

如何解决此问题?

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

Lumen没有您在Laravel中可能熟悉的
公共路径()
,无法轻松获取公共文件的路径

重新实现它的最简单方法是向项目中添加一个名为的包,该包添加各种缺少的帮助程序(包括
public\u path()
),以及添加Lumen中缺少的供应商发布命令

或者,如果您不想添加第三方软件包,只需在应用程序目录中创建一个名为
helpers.php
的文件,然后在
composer.json
文件中,在“自动加载”部分添加以下内容,并运行
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);
    }
}

file\u get\u contents()
相对于您的php脚本执行(并且您不会在正确执行真正代码的地方做太多修改,因为laravel是如何工作的)如果您有该路径的话。使用
base_path()/test/test.xml'
@Tezla这不管用,你为什么不读一下,它在公共路径中,你可以
public\u路径('test/test.xml')
。为了确保这一点,请点击
dd
这段代码并查看路径-如果它是正确的,那就是你的答案。否则,祝你好运:)-显然,我之前的评论不起作用,我假设测试目录与
app
config
,…,
供应商一起位于根目录下。谢谢。我有点希望突然。。。我使用Lumen,
public_path()
没有在这个框架中实现,目前(Lumen 5.1.6)我的回答很糟糕,很好!我选择了在我的生活中更安全的解决方案opinion@Zl3n你怎么知道这样更安全?它可能使用相同的东西。我猜它的作用差不多,自动加载字符串不应该改为“helpers.php”吗?删除应用程序作为composer.json驻留在应用程序中?
<?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);
    }
}