Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Error thrown when including and requiring files

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,

like image 804
Rashad Avatar asked Jul 03 '12 00:07

Rashad


1 Answers

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

like image 146
Sergii Stotskyi Avatar answered Oct 26 '22 23:10

Sergii Stotskyi