Keeping track of DIRs when Including scripts. -[RESOLVED]-
Whats the best way to keep track of what Directory you are in when including files.
For example I have the script being run: /index.php
But inside it it will include a file /inc/user.inc.php
Then inside that file it wants to include /inc/error.inc.php
Kinda simple apart from if I include /inc/user.inc.php into the file /admin/index.php then the line inside /inc/user.inc.php that includes the error include will be wrong, because its a relative include.
I must use relative includes because I want this to be able to run on different servers and they may not be hosting from the same DIR.
I had a thought of having a variable in every script that will hold which DIR it is in but didn't know if that was tacky. Also if the files were moved to another DIR this falls down. What is the easy way to determine the DIR the script is being run from relative to the Hosting DIR.
Thanx
Re: Keeping track of DIRs when Including scripts.
The best way to do it is to include everything relative to the base directory of your application. The reason I say this is becuase the default include path is the current directory of the of the file you are calling include from. If the file doesn't exist in that directories include path, it then searches the include path for the scripts current working directory.
Re: Keeping track of DIRs when Including scripts.
thanx, but what if you had a file /admin/admin.php and it also wants to include /db/mysql.php and /classes/site.php. This is what i'm facing. I think my solution is to pass a variable which will represent how to get to the Root of the application. So in this case inside admin.php it would equal "../" but inside index.php it would be an empty string. This should work but could be a bit messy.
My .inc.php files only ever include another file from inside functions so it is easy enough to add an extra parameter saying where the root is of the app.
Thanx for you help though :).
Re: Keeping track of DIRs when Including scripts.
Now I'm confused - I've just been testing this on my server and the behaviour is a bit inconsistant.
:confused:
Give us a few.
Re: Keeping track of DIRs when Including scripts.
I'll try to simplify what I get:
Files:
/index.php
/user/staff.php
/inc/bob.inc.php
/inc/sue.inc.php
File: /index.php
include "inc/bob.inc.php";
// More code...
File: /inc/bob.inc.php
include "inc/sue.inc.php"; // Expected usage from /index.php
// More Code...
File: /user/staff.php
include "../inc/bob.inc.php";
// More Code...
Running /index.php works fine.
Running /user/staff.php fails, it tried to find /user/inc/sue.inc.php
Re: Keeping track of DIRs when Including scripts.
And my Hacky solution:
File: /index.php
$Root = "";
include $Root."inc/bob.inc.php";
// More code...
File: /inc/bob.inc.php
include $Root."inc/sue.inc.php";
// More Code...
File: /user/staff.php
$Root = "../";
include $Root."inc/bob.inc.php";
// More Code...
Both will now run fine. I guess I don't need the $Root used int eh includes for the actual scripts being run (index.php & staff.php) but I guess if I use that and check if $Root has been defined already i'd be able to include anything from everywhere and it will work.
Re: Keeping track of DIRs when Including scripts.
After a quick look on the documentation for include on PHP.net I found this:
Quote:
Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is ., current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/ and then in /www/include/. If filename begins with ../, it is looked only in include_path relative to the current working directory.
The first place when using a relative path that PHP looks is in the current directory of the actual file. If it is not found then the current working directory is then searched, this is return by getcwd(). The exception as explained above is with the relative path ../. If use this then only the current working directory search.
Does your staff.php file change the current working directory at any point during execution. If not then I am not sure why its not working.
Re: Keeping track of DIRs when Including scripts.
Quote:
Originally Posted by Electroman
And my Hacky solution:
File: /index.php
$Root = "";
include $Root."inc/bob.inc.php";
// More code...
File: /inc/bob.inc.php
include $Root."inc/sue.inc.php";
// More Code...
File: /user/staff.php
$Root = "../";
include $Root."inc/bob.inc.php";
// More Code...
Both will now run fine. I guess I don't need the $Root used int eh includes for the actual scripts being run (index.php & staff.php) but I guess if I use that and check if $Root has been defined already i'd be able to include anything from everywhere and it will work.
Thats how I'm doing it. :)
Re: Keeping track of DIRs when Including scripts. -[RESOLVED]-
I'm not a big fan of using variables to keep track of the directory. Keep it all well organised and you shouldn't have a problem.
Re: Keeping track of DIRs when Including scripts. -[RESOLVED]-
BTW from what visual posted if I just put include "sue.inc.php"; in bob.inc.php then it actually works. Seems a little strange but using ../ will make it switch to only search relative to the CWD (where the script being run is). Whereas if you just write a relative address without any ../ in then it will first search where the file the code is in (in this case the inc folder) then the CWD after that if its not found. Pretty annoying if I need to go back a DIR but luckily al my includes are in the same DIR :)