Base Directory Change Issue
In my app, I'm using an AJAX call to retrieve information, which requires the inclusion of a db_connect.php file. db_connect.php requires other files for it to work.
If I include db_connect.php from the AJAX file, naturally, errors are returned from includes within the db_connect.php file because db_connect.php's includes are relative to the base directory of my application.
How can I change the working directory to th开发者_高级运维e base directory of the application, so the included files will function properly?
What I've tried is using the chdir
function:
echo getcwd() . "<br/>";
chdir("../../");
echo getcwd();
This correctly outputs:
/my/webserver/app/lib/ajax
/my/webserver/app
However, the include errors act like I haven't just changed the directory:
Warning: include_once(functions/clean_string.func.php) [function.include-once]: failed to open stream: No such file or directory in my/webserver/app/lib/ajax/ajax.php on line 8
What am I doing wrong?
Then use absolute paths.
For example in a config file, that resides in base of the app, define a base path that is absolute.
define("BASE_PATH", dirname(__FILE__)); //This now is a ABSOLUTE path to a dir that this file is in.
Then use:
include BASE_PATH . "/dir/dir/file.php";
Then, you just have to include the config file in every script at then top relatively from where you are in the app and then just use BASE_PATH in all other includes.
getcwd()
does not return your include path; instead, try something like this:
chdir("../../");
include(getcwd()."/functions/clean_string.func.php");
精彩评论