简体   繁体   English

带有流明的file_get_contents

[英]file_get_contents with Lumen

I have this code into a function (php class) : 我有这个代码到一个函数(PHP类):

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

If I go to mydomain.local/test/test.xml , I get the working xml code. 如果我去mydomain.local/test/test.xml ,我会得到正常工作的xml代码。

But with file_get_contents , I get this error : 但是使用file_get_contents ,我收到此错误:

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

How to solve this ? 怎么解决这个?

You are passing an absolute path to the function that is relative to the server's base directory. 您正在传递相对于服务器基本目录的函数的绝对路径。 This is not necessary the same base directory for the URL. 这不是URL的相同基目录。 Try passing a path relative to the current executing script. 尝试传递相对于当前执行脚本的路径。

Lumen doesn't have the public_path() that you might be familiar with in Laravel to easily grab the path to a public file. Lumen没有你在Laravel中熟悉的public_path()来轻松获取公共文件的路径。

The simplest method for re-implementing it would be to add a package called irazasyed/larasupport to your project which adds various missing helpers (including public_path() ) as well as adding the vendor publish command that is missing from Lumen. 重新实现它的最简单方法是在项目中添加一个名为irazasyed / larasupport的包,它会添加各种缺失的帮助程序(包括public_path() )以及添加Lumen中缺少的vendor publish命令。

Alternatively if you do not wish to add a third party package simply create a file in your app directory called helpers.php and then within your composer.json file add the following within the "autoload" part and run composer dump-autoload to refresh the autoloader cache: 或者,如果您不想添加第三方软件包,只需在app目录中创建一个名为helpers.php文件,然后在您的composer.json文件中,在“autoload”部分中添加以下内容并运行composer dump-autoload刷新自动加载器缓存:

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

Then within helpers.php add the following content: 然后在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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM