PDA

View Full Version : base directory path


LadyYepperz
11-16-08, 17:14
ok.. im working in php and in a script i need to include my base directory path.
where da heck do i find it?? im trying to use the php include() function on my html pages


<?php
//ive tried...

echo getcwd();

//and...

echo phpinfo();

//and...

echo ($_SERVER['DOCUMENT_ROOT']);

//and...

echo $_SERVER["SCRIPT_FILENAME"];
?>

ok... tho ive tried all of those to find my base directory.... with no success... ive decided to try yet again.

then i thought, "maybe the problem is in my script"
so, here are those...

definition.inc.php
<?php
define("BASE_DIRECTORY", "/www/oxyhost.com/l/a/d/ladyyepperz/htdocs/");
ini_set("include_path", BASE_DIRECTORY."includes/");
?>

body.html
<?php
include("definition.inc.php");
include("header.inc.php");
?>

can you tell me... is the problem in my scripts or still in the path??

p.s. that orange in the php tags is all bad! Yikes! MG needs to rethink the comment color.

w3d
11-16-08, 17:56
$_SERVER['DOCUMENT_ROOT'] would normally be your 'base directory' or web root. You would, however, need to append a slash to this in order to build a complete path:
include($_SERVER['DOCUMENT_ROOT'].'/includes/myinclude.php');

HOWEVER, are you on IIS? In which case, DOCUMENT_ROOT is not available! But it can be worked out by messing around with some of the other superglobals... SCRIPT_NAME and SCRIPT_FILENAME etc. This may depend on what your server is returning.

What does phpinfo() show?

LadyYepperz
11-16-08, 18:31
$_SERVER['DOCUMENT_ROOT'] would normally be your 'base directory' or web root. You would, however, need to append a slash to this in order to build a complete path:
include($_SERVER['DOCUMENT_ROOT'].'/includes/myinclude.php');
$_SERVER['DOCUMENT_ROOT'] returns /www/

HOWEVER, are you on IIS?
Where do i find that?

What does phpinfo() show?
SCRIPT_NAME returns /info.php
SCRIPT_FILENAME returns /www/oxyhost.com/l/a/d/ladyyepperz/htdocs/info.php
include_path returns .:/usr/share/php (dont know if this is important or not)
SCRIPT_URI returns http://ladyyepperz.oxyhost.com/info.php

is there anything else you think might be important?

PC-XT
11-16-08, 19:51
You might try these, one of them may work?/l/a/d/ladyyepperz/htdocs/
/(That last one is just a slash by itself.)

w3d
11-17-08, 08:15
$_SERVER['DOCUMENT_ROOT'] returns /www/

"HOWEVER, are you on IIS?"
Where do i find that?

SCRIPT_NAME: returns /info.php
SCRIPT_FILENAME: returns /www/oxyhost.com/l/a/d/ladyyepperz/htdocs/info.php

IIS (Internet Information Server) is Microsoft's webserver more commonly found on Windows servers. As opposed to the open source Apache webserver more commonly found on Linux servers.

The fact that DOCUMENT_ROOT is set at all does imply you are on Apache (and the server path looks Linux-like). However, the DOCUMENT_ROOT that is being returned is not what you are expecting! (This is probably just down to how the server is configured and if you are on a shared host (or part of a larger organisation) there is probably little you can do about it!)

Presumably the DOCUMENT_ROOT you are after is...?
/www/oxyhost.com/l/a/d/ladyyepperz/htdocs

You will need to somehow set this at the top of each script that includes files. But you want to avoid hard-coding this path everywhere in case you move servers at a later date?

You could do something like:

Create a config.php file in your webroot (document root / base directory - whatever you want to call it)
<?php
// config.php
// Set whatever is the base directory
// This replaces SCRIPT_NAME in SCRIPT_FILENAME with an empty string (two single quotes)
// to hopefully return just the base directory.
// (You could also join this onto the include_path if you wanted)
define('BASE_DIRECTORY',str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']));
?>

Use the auto_prepend_file directive in .htaccess to append this file to the beginning of every file:

# .htaccess
# Needs to be hard coded unfortunately - but only once
auto_prepend_file /www/oxyhost.com/l/a/d/ladyyepperz/htdocs/config.php


Although the $_SERVER[] array is reasonably consistent between similar servers... it is not guranteed to be the same!

Hope that helps.

LadyYepperz
11-17-08, 19:02
@W3D

thank you for that information as i am still a student of PHP and server side scripting... I am indeed on an Apache server... I will take what you've said into consideration and implement your suggestions but....

after renaming my body.html to body.php.. i received the following errors...
maybe you can help me understand what they mean...

Warning: include(definition.inc.php) [function.include]: failed to open stream: No such file or directory in /www/oxyhost.com/l/a/d/ladyyepperz/htdocs/body.php on line 2

Warning: include() [function.include]: Failed opening 'definition.inc.php' for inclusion (include_path='.:/usr/share/php') in /www/oxyhost.com/l/a/d/ladyyepperz/htdocs/body.php on line 2

what is it saying??
looks to me like i need to use include_path='.:/usr/share/php' as my base directory path (document root) or as my include path... but im really not sure... so before i do anything ill wait to see what you think.

LadyYepperz
11-17-08, 19:43
i used the following script

echo get_include_path();

and it returned...

.:/usr/share/php/www/oxyhost.com/l/a/d/ladyyepperz/htdocs/includes/

is this what i should be using?... im so freakin confused! if this is my include path... it still doesnt tell me what my document root is... :confused

w3d
11-18-08, 17:22
is this what i should be using?

The include_path is the set of paths (yes, there can be more than 1, separated by ':' on Apache/Linux) that PHP looks in when you try to include a file. The path/file you specify in the include() needs to be relative to one of these paths (unless you specify a root-relative path - which is a bit impractical). Usually the first path specified in the include_path is the '.' (period/dot) indicating the current working directory (CWD). Many people probably just include files relative to the CWD, which is fine for a few casual includes, but in a more involved webapp with reusable components this could become an unnecessary headache to maintain. So, you can set the include_path to your DOCUMENT_ROOT (or specifically to your include folder as you are doing in your OP).

after renaming my body.html to body.php.. i received the following errors...

(Sorry, I didn't spot the .html extension earlier) As you probably realise, without the .php extension your file is not (normally) parsed by PHP and consequently your <?php ?> blocks do not get executed.

Those warnings are just saying it can't find your includes. The path is wrong.

I think in your case, it's a simple matter of chicken and egg... you are trying to include "definition.inc.php" assuming that you have already set the include_path - which you do in "definition.inc.php"! There lies your problem.

Since your body.php file is already in your webroot and your includes folder is a sub folder off your webroot then you could simply include your files relative to the CWD (and not bother setting your include_path, since the include_path already includes the CWD) as mentioned above:
<?php
// body.php
include("includes/definition.inc.php");
include("includes/header.inc.php");
?>

To get it to work the way you were wanting you would need to insert the contents of definition.inc.php at the top of body.php - which is a bit impractical if you are hard coding the include_path and not using the $_SERVER[] variables. OR you could use the method I mentioned below to calculate your document root using str_replace() and $_SERVER['SCRIPT_FILENAME'] etc. OR use the auto_prepend_file directive in your .htaccess file to automatically prepend your definition.inc.php to every file (ie. body.php) - sorry, I missed the php_value instruction in my post below!
# .htaccess
# Needs to be hard coded unfortunately - but only once
php_value auto_prepend_file /www/oxyhost.com/l/a/d/ladyyepperz/htdocs/includes/definition.inc.php

and keep your body.php file as it is.

The above (htacces) code would go in the ".htaccess" file in your webroot (it may not already exist) and would effect the webroot and every sub folder. Do you know about .htaccess files?

LadyYepperz
11-19-08, 15:03
Do you know about .htaccess files?

im very, VERY new to php and ALL server side scripting...
in fact im learning as i build this site
so... no... i dont know about .htaccess files.
i am totally confused and lost... :cry
i want to thank you for taking the time to help me..
and i ask that you please be patient with me.

LadyYepperz
11-19-08, 15:23
i checked out this page on .htaccess...

http://httpd.apache.org/docs/1.3/howto/htaccess.html#what

and it doesnt sound like something a newbie should mess around with... :(
and to top it off... i dont know where i would find it or if i have to create it.... or what

LadyYepperz
11-19-08, 15:48
[QUOTE=w3d;1241892]I think in your case, it's a simple matter of chicken and egg... you are trying to include "definition.inc.php" assuming that you have already set the include_path - which you do in "definition.inc.php"! There lies your problem.

Since your body.php file is already in your webroot and your includes folder is a sub folder off your webroot then you could simply include your files relative to the CWD (and not bother setting your include_path, since the include_path already includes the CWD) as mentioned above:
<?php
// body.php
include("includes/definition.inc.php");
include("includes/header.inc.php");
?>

[QUOTE]

now this makes sense to me... but...
definitions.inc.php is in the same directory as body.php
the include files are in the "includes" sub-folder, i.e.
header.inc.php, footer.inc.php, etc..
so... the body.php would look like this...
<?php
// body.php
include("definition.inc.php");
include("includes/header.inc.php");
?>
that is clear to me... but...

its the definition.inc.php, i think, that is giving me the problem...
how do i set up this file so my includes work?

PC-XT
11-21-08, 17:51
try <?php
// body.php
include("./definition.inc.php");
include("includes/header.inc.php");
?>(add "./" to the first include)

LadyYepperz
11-22-08, 13:48
ty!! that worked!! no more problem!:-D

w3d
11-22-08, 15:25
(add "./" to the first include)

May be to both, since both includes look like they should be relative the current directory? (a single dot in the path indicates the current working directory - CWD)

But... what really is the current include_path?

You stated earlier that:
echo get_include_path();
and it returned...
.:/usr/share/php/www/oxyhost.com/l/a/d/ladyyepperz/htdocs/includes/
Which includes the CWD (the dot before the colon at the start of the include path), so having to specify './' in your include() shouldn't be necessary...?

However, in your original code snippet:
define("BASE_DIRECTORY", "/www/oxyhost.com/l/a/d/ladyyepperz/htdocs/");
ini_set("include_path", BASE_DIRECTORY."includes/");
...would ordinarily replace the current include_path completely with what you specify (removing the CWD ".:" part).

In order to add to the current include path (rather than replace it entirely) then it is more usual to do something like:
ini_set("include_path", BASE_DIRECTORY . "includes/" . PATH_SEPARATOR . ini_get("include_path"));
Which adds your choice of include path to the beginning of the current include path.

(I hope all this isn't getting too confusing!?)

w3d
11-22-08, 17:28
ty!! that worked!! no more problem!

Glad you got it sorted. (I didn't see your post earlier!)

I'm still curious as to what your include_path is being reported as {echo get_include_path();}, before and after including your definition.inc.php ? Thanks. :)