Hi,

I have a file that connects to a database, database.php. I also have a file for defined constants, config.php, and it is stored in the top level directory because I'm going to use those constants on the main site and when I edit them, I don't want to edit two different files. So in database.php I added require_once('../../config.php'). Then, when I want to connect to the database in a particular file (home.php), I just add require_once('includes/database.php').

The problem I'm having is the path in the require_once('../../config.php') code in the database.php file breaks depending upon where the require_once('database.php') code is being called from, which is the case below:


It works if require_once('../../config.php') is instead require_once('../config.php'). Looks like when I require a file and that required file contains other required files, it uses the main file's directory path instead. I hope I explained my problem clear enough. To reiterate, I basically want the path to the config.php file (again called from the database.php file to be resolved automatically so I can require database.php from anywhere within the 'cpanel' directory.

Hi Wallace,

What to note is that PHP includes files relative to the current file being executed. ie: current working directory.

So the path does not depend on which file you are calling another file from, but from the file being executed by PHP (the first file PHP started executing).

There are different ways to accomplish the task keeping relative file paths simple.

1) You could add the base directory your application is in into your include path. http://www.php.net/manual/en/ini.core.php#ini.include-path This way you can make all includes() relative to that path.

2) You could load all files using absolute paths. To do this you can add the base of your application to the config.php file. eg: define('APP_BASE_DIR', '/path/to/base'); You can also retrieve the base path dynamically with dirname(__FILE__) if the config.php file is in the base.
The only issue here, is that you have to make sure you include the config.php file only in the PHP file being executed.

3) You could only include files from the file being executed, as it is the only path you can be sure off. This has the disadvantage that you have to know every file you need before hand.

4) You can define an autoload function and include files after their class names.

function __autoload($class_name) {
    require_once $class_name . '.php';
}

5) You can use a single file as the point of entry into your application. It then delegates which files are included based on the task to be executed.

Eg: You can have an index.php that will delegate files to include based on the "task" variable. So index.php?task=home would include() home.php from within index.php. With this method, all include paths only have to be relative to index.php since you will never load any other file directly.
This method is one of the components of the popular Model View Controller pattern. In the MVC pattern it is the Controller.

6) You can use a function or class designed specifically for loading files. This is also common in MVC frameworks. An example is a class that searches common directories in your application for the file include in a cascading pattern. This has the benefit that you can override files just by adding them to the correct location. Sometimes this function is built into the __autoload() function, but it can be separate or built into the factory pattern also common in MVC frameworks.

There are probably other methods I haven't mentioned.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.