PHP: Error thrown when including and requiring fil

2019-06-23 08:57发布

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条回答
萌系小妹纸
2楼-- · 2019-06-23 09:33

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

查看更多
登录 后发表回答