Reputation:
Imagine I have two directories, foo and bar in a parent directory, and these directories contain some PHP files:
/
/index.php
/foo
/foo/functions.php
/foo/stuff.php
/bar
/bar/doodie.php
Let's say:
functions.php
includes stuff.php
index.php
includes functions.php
doodie.php
includes functions.php
Based upon this structure, when index.php
is run, it will look for /foo/functions.php
in the /foo
directory. However, stuff.php
must be included using the relative path according to the location of index.php
. This works, in the case of when index.php
is run. It doesn't work when doodie.php
is called because functions.php
looks for stuff.php
in the /foo
directory, which doesn't exist when the location is /bar
.
Is it possible to include files according to where they reside instead of where they are called from, without using absolute paths (and session variables)?
I apologize if this wasn't clear, it was really tough to describe this...
Upvotes: 0
Views: 113
Reputation: 6550
I would suggest you go the route many MVC goes. It is easy yet reliable. Just redirect all requests to one file in form of index.php?path=database.php (let say bootstrap.php) that is in root directory and then define paths. Here is example of my file that I use in my home made MVC framework.
/**
* BASE_PATH is for server side inclusions.
* BASE_URL is for client side inclusions (scripts, css files, images etc.)
*/
define("DS", DIRECTORY_SEPARATOR);
define("BASE_PATH", dirname(realpath(__FILE__)).DS);
define("BASE_URL", dirname(realpath($_SERVER["SCRIPT_NAME"])).DS);
define("APP_PATH", BASE_PATH."application".DS);
//$url = $_GET['base_url'];
/**Include core file which loads all necessary files */
require_once (BASE_PATH."core".DS."core.php");
and here is core.php (only one line as it have many lines)
//Load config files
require_once(BASE_PATH."config".DS."config.general.php");
Upvotes: 0
Reputation: 4221
You can do this:
Create a file in the base folder; in this file define these variables
define('root', SERVER['DOCUMENT_ROOT']); this ensures the root folder is obtained
define('foo', root . '/foo/'); this defines the path to the foo folder
define('bar', root . '/bar'); this defines the path to the bar folder
Now, in whatever file you need to call, include the base file, and you have access to any file in any of those folders.
So, if you are already in /bar/doodie.php, just include(foo . 'functions.php'), and vice versa.
Hope that helps.
Upvotes: 1
Reputation: 50009
You should use absolute paths.
Of course you don't need to include the full path every time. The usual process is that you define a constant called, for example, APP_PATH and set the absolute path of the application in your bootstrap.
After that, whenever you want to include a file, append the APP_PATH constant in your include calls. This will solve your problem.
You could also look at the include_path option, this depends on how your files are located - http://php.net/manual/en/function.set-include-path.php
Upvotes: 1