Forum Moderators: coopster

Message Too Old, No Replies

Reference to root and sub directories

$_server['document_root']

         

mvaz

10:07 pm on Feb 25, 2010 (gmt 0)

10+ Year Member



I have this test site that has a subfolder called includes; wherein I have my include files.

Now, when I need to include any of these files on other files, if I use include_once $_SERVER['DOCUMENT_ROOT'] ."/includes/file_name.php";, it works fine.

But if I use "/includes/file_name.php" it won't work.

However, if I use "../includes/file_name.php" it works fine.

Now, any suggestions as to why this variance, and which is the best approach and why?

Many thanks for your suggestions and opinions.

Readie

10:23 pm on Feb 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In HTML etc, / designates the root of the web directory
In PhP, / designates the root of the drive partition

Probably best to use $_SERVER['DOCUMENT_ROOT'] as it's an absolute, whereas ../ is a relative.

Matthew1980

10:40 pm on Feb 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there mvaz,

Just a thought, but when you use:-

"/includes/file_name.php" it won't work.

I think it would work if you had:-

"./includes/file_name.php" in it, meaning the current/working directory, that is if it is a folder tree based in the website root..

Personally I always define the <base href="http://www.somesite.com" /> in the head of the main index file, then, at least there is a reference to it.

Another good way is to:-

define('PATH', dirname(__FILE__). "/"); //current dir..

include(PATH."includes/sign-up.php");

Just a couple of ways that I tend to employ.

Cheers,

MRb

g1smd

12:23 am on Feb 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The base tag is usually irrelevant because the file content is included via an internal filesystem call, and not via an external HTTP request.

It is a browser that evaluates the base tag content, not the server.

Matthew1980

8:45 am on Feb 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there g1smd,

WRT <base>, I actually reference a Constant there that contains the complete site url including the last / so that if I need it elsewhere I just reference the Constant, then I know its the full path being used rather than just ../ or ./

Thats just my preference, I did find that without the <base> defined, the .htaccess didn't function.. This may be an obvious thing, but I never really understood why, I'm just glad as it works!

Cheers,

MRb

mvaz

9:37 am on Feb 26, 2010 (gmt 0)

10+ Year Member



Guys, thank you for your wonderful responses, opinions and suggestive ideas with reasons.

This has given me quite a bit of idea and options of how to workaround this issue.

Once again, thank you, and if I have any problems, I know where to come back to.

penders

11:09 am on Feb 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



See also this thread (which also includes a link to the forum library):
Controling include path using config.php [webmasterworld.com]