Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Include Paths

Status
Not open for further replies.

ralphiooo

Programmer
Oct 23, 2005
64
GB
Hi, for the last few years i've not thought too much about include paths, before i used to put require './includes/common.inc.php'; at the top of every file which is situated in the root. This file also references files with the same include path eg require './includes/functions.inc.php';

Now I have moved the admin file to it's own folder called admin and changed the require statement to require '../includes/common.inc.php'; but now it can not read the correct path to the files referenced in the common.inc.php file.

I'd appreciate if someone could give me a solution which will work (even if I use mod rewrite and the file is not in the folder in the url - if you get me) and is not too messy.

Appreciate the help. Thanks
 
mod_rewrite does nothing here. PHP's filesytem functions to not operate through the web browser.

What I would do is set the include_path value for this virtual server to have in it the path to your "include" directory. Then all your scripts could just do:

include 'functions.inc.php';

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Hi cheers for the response, is their an easier solution which doesn't involve modifying server files as that would be fine working locally but I don't always have access to the server files. Cheers
 
From the PHP documentation on include():
If filename begins with ./ or ../, it is looked only in include_path relative to the current working directory.
In other words, you can't do it that way. You have to modify include_path.

Fortunately, you can modify include path pretty much anyplace. In addition to the server config files, you can do it in a .htaccess file or in code with
Code:
ini_set("include_path", "/path/to/include/files");
 
Hi, cheers for your help. I checked up on the include path stuff and seems alot clearer now. Since ./ and ../ looks for an include path relative to the current working directory I declared a constant at the top of my /admin/index.php called ROOT_PATH and set it to "./../". I then did the same for the files in the root folder, eg for /index.php and set it to "./". Then for each file I called the include file with:

require ROOT_PATH . 'includes/common.php';

Finally in the common.php file I refered the include the same way, eg:

require ROOT_PATH . 'includes/config.php';

Hope that can help someone as I think it is quite a tidy solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top