PHP: Error thrown when including and requiring files

时光毁灭记忆、已成空白 提交于 2019-12-10 14:36:49

问题


I tried to create a bootstrap file, but whenever I try to include or require it in another file an error like this keeps showing up:

Warning: require_once(../folder/file.php) [function.require-once]: failed to open stream: No such file or directory in...

Fatal error: require_once() [function.require]: Failed opening required '../folder/file.php' (include_path='.:') in...

To paint the entire scenario: I have a bootstrap file, load.php. In it, I connect to the config file at ../config/config.php. Above the load file I have a database class file at classes/database.php. In the database file I wrote this if-statement:

           if ( file_exists('../load.php') ) {
               echo 'load.php: It exists';
               require_once('../load.php');
           } else {
                  echo 'load.php: It does not exist';
                  }

In the load file I wrote a similar if-statement, but checking if the config file exists. Surprisingly, when I load the database.php file I get:

load.php: It exists
config.php: file doesn't exist

But when I load the load.php file and I get:

config.php: It exists

Here is where my files are located:

Root Directory: /Library/WebServer/Documents/project

config.php file /Library/WebServer/Documents/project/config/config.php

load.php file: /Library/WebServer/Documents/project/includes/load.php

database.php file: /Library/WebServer/Documents/project/includes/classes/database.php

I need help with this bug. Thanks,


回答1:


use dirname(__FILE__) . '../relative/path/from/file/in/which/opened/to/config.php'. From PHP 5.3 there is also __DIR__ constant



来源:https://stackoverflow.com/questions/11303033/php-error-thrown-when-including-and-requiring-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!