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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php include path relative, absolute

Status
Not open for further replies.

mwpclark

Programmer
Mar 14, 2005
59
US
I have been trying to understand the logic of included files in php.

I have these files:
index.php
inc/footer.php
inc/navbar.php
inc/toplinks.php

index.php sez:
include ('inc/footer.php');

footer.php sez:
include ('navbar.php');
include ('inc/toplinks.php');

The navbar.php file loads correctly and grabs the values passed from index.php. It also works where the include sez:
include ('inc/navbar.php');
There is only one file named navbar.php.

There is a second file named toplinks.php in the root directory.

If footer.php sez:
include ('toplinks.php');
it calls the file from the root directory.


Does php search all possible directories (the directory of the original including file, as well as the directory of the included file?

Nothing works if it sez:
include ('/inc/toplinks.php'); or include('/inc/navbar.php');

Is there a difference between double quotes and single quotes?

Thanks
Mike
 
the includes work from the directory of the script from which the script was first instantiated. in your example they will all be called as if the current working directory was the directory of index.php.

i would not expect the example of include 'navbar.php' to work unless the path ./inc is part of the include path set in php.ini

there is a great difference between single and double quotes. read up on it in the manual.
 
I know it is very counter-intuitive of PHP, but here is how it works:

First, the file is looked up with respect to the CALLED (the first) file. NOT with respect to the current file.

If that fails, The file is looked up with respect to the current file (which is intuitive).

If that also fails, the path is used as configured in php.ini.

So basically you find out when you have the same file name in different places in the directory tree, as you found out.

Here is how I overcome this:

Code:
require_once(dirname(__FILE__) . '/inc/toplinks.php');

(Mind the / before inc, as the dirname function returns a path without a trailing slash). This construct will get the file relative to the current file.


+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Thank you, I think the explanation from DonQuichote pretty much describes the behavior I observed.

I understand about parsing and non-parsing of variables with single and double quotes, however I think in this situation there is no effect as there are no variables present and the code is too simple to cause any parsing slowdown.
 
@DonQuichote

i'm surprised that include works the way you say. can you point me to the manual reference or source code line that backs this up.

note: i am NOT disagreeing with you. however if the behaviour is not documented it cannot, imv, be relied upon. i would be very pleased to see if the behaviour were documented somewhere as, although lazy, it would simplify certain coding headaches.
 
I did not look into the source code. However, the manual is not too clear on this. I can remember that the manual was more clear in the past.

See
It says:
For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

The keyword is the requested file. A comment of tells this also.

My experience is that if the file relative to the requested file cannot be found, the file relative to the current file is also tried. That may have been changed in a later PHP version though. After I found out that paths were relative to randomness (no code line knows what other file called them), I always used the above construct to correct the behaviour of PHP.

It seems I was wrong though in fact that the configured path is used third. According to the manual, it is not used at all if a path is given.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top