Page 1 of 1

php includes

Posted: Tue Oct 12, 2004 7:52 am
by jessevitrone
I'm having some trouble with include, and I'm not sure if there's a way around it.

Here's a simplified version of my directories and files:

test.php
actions/login.php
classes/config.php
classes/database.php

My config.php includes database.php.

When I try to have test.php include config.php, config.php can't find database.php because it's looking in the directory that test.php is in, not it's current directory.

Is there some way around this? I realize that I can just have test.php do the database.php include before I include config.php, but I was hoping to avoid doing that.

Thanks for any suggestions,
Jesse

Posted: Tue Oct 12, 2004 8:02 am
by jessevitrone
Is there some way to include a file with an absolute path, and not a relative path? Absolute with respect to the web app though, not to the server where I have to start it off with something like /usr

Thanks.

Posted: Tue Oct 12, 2004 8:21 am
by twigletmac
If $_SERVER['DOCUMENT_ROOT'] is set you could use that to build more absolute paths.

Mac

Posted: Tue Oct 12, 2004 8:26 am
by djot
-

Yes there is, you can try $ _SERVER["DOCUMENT_ROOT"],

But it has some disadvantage, because it's behaviour is different on each server. Sometimes it returns

e.g
/var/www/user/~username/
sometimes only
/~username/

,so you will run in trouble than again.

That is my experience with document_root.

Another way is to set a global variable, that holds if you include from root or from upper folder and adjust the path then with an if clause.

like

Code: Select all

<?php

// include from root (./)
if ($yourglobalvar == "root") { include_once('./classes/config.php'); }
// include from e.g. ./actions/ 
else { include_once('../classes/config.php'); }

?>
Not very nice, but that is safe on all servers.


djot
-

Posted: Tue Oct 12, 2004 8:27 am
by jessevitrone
yeah, seems like there's another trick using

include(dirname(__FILE__) . '/path/relative/file_to_include.php');

to include it relative to the current file, and not to the original file.

Thanks for the suggestion.