Jump to content

Include path problem


djp1988
 Share

Recommended Posts

Hello, I have an include path problem, I have my web directory that contains all my website, and I have a php_class directory where I put my classes, from the web directory I inlucde the classes by doing the following:require_once('../php_class/my_class.php');I have a script in this web directory (lets call it init.php) that includes all the classes I need for my site, and on each page I include this one file, which fetches the others...However, within my web directory I have another directory, lets call it /Dir1, so from within here when I include my init.php, it tells me that the include statements are not finding the files, I don't really know what I should do.I thought that when I include a file, it evaluates it therefore including any files from that script before being placed into the main script...

Link to comment
Share on other sites

I appreciate that my explanation is a mouth full, Consider the following:/classes | -class1.php/www | -init.php | -script2.php |/Dir1 |-script1.php/www/init.php has require_once('../classes/class1.php')/www/script2.php has require_once('init.php')/www/Dir1/script1.php has require_once('../init.php')When I open script1.php I get this message:Warning: require_once(../classes/class1.php) [function.require-once]: failed to open stream: No such file or directory in /www/init.php on line 1But with script2.php it's all fineWhat should I change ?

Edited by djp
Link to comment
Share on other sites

No matter how many nested levels of include file you have, all their paths will be understood to be relative to the script that is actually running. It only seems that init.php is looking for class1.php. That is not the case, because init.php is not running. script1.php is looking for class1.php, and it cannot find it in the path that init.php provides.Your best bet is to use absolute paths for all your include files. Every path should begin with "/" so that PHP knows to begin the path at the root directory. This way you'll never have an error. (Just don't move your include files!)

Edited by Deirdre's Dad
Link to comment
Share on other sites

I avoid absolute paths, because it can be very difficult to move the application or install it on another server.
Yep. I usually define an absolute path in a config setting somewhere so changing it is less of a hassle, but letting PHP determine the path is probably the best/cleanest way to do it.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...