Page 1 of 1

Site root and constantly changing working directory

Posted: Sun Dec 05, 2004 9:02 am
by firkinfedup
Hi there,

I have a problem regarding obtaining the correct path from any page in my web site. Currently I have PHP files being included inside of nested directories, for example

foo/bar/foobar.php
^
This includes the file @ php/barfoo.php

The problem is that when foobar.php is loaded the current working directory changes. So any file references I have in barfoo get broken. I *was* under the impression that following applied,

Current page = "http://www.spoon.com/dir/page.html"

"/dir2/page.html"
^
Would become "http://www.spoon.com/dir2/page.html"

"dir3/page.html"
^
Would become "http://www.spoon.com/dir2/dir3/page.html"

"../page4.html"
^
Would become "http://www.spoon.com/page4.html"

But this doesn't seem to apply to PHP, only HTML. *Confusion*, so that leaves me asking, is there an escape character to drop to the document root? I would like all includes to be relevant from the document root as well as any files I open. Thanks *loads* in advance!

BTW. I've tried the $_SERVER['DOCUMENT_ROOT'] variable but this just returns an error.

Nick.

Posted: Sun Dec 05, 2004 9:04 am
by timvw
time to discover what php's include_path is.

Works for includes

Posted: Sun Dec 05, 2004 9:08 am
by firkinfedup
Hi Jason,

ACK, I've used that at the beginning of each function that uses an "include" statement. But that doesn't help me when loading files, for example I load XML files in some scripts, they aren't loaded via an include statement but the current working directory still applies to them. I was hoping something like,

"//foobar/file.xml"

Would drop me down to the root, I'm sure this is really easy to do, I'm just crap at PHP obviously! :?

Nick

Posted: Sun Dec 05, 2004 9:20 am
by kettle_drum
Do as timvw suggests or place a variable in front of each include, so that you can just change the one variable in your script and that will adjust all of your includes.

Code: Select all

$PATH = 'some/path/';

include_once($PATH."more/path/file.php");

Hi there

Posted: Sun Dec 05, 2004 9:34 am
by firkinfedup
Sorry, got the guys name wrong, the email from "Jason" confused me LOL.

I acknowledge that includes can be done in this way, but as I said in the last post it's not the end of my problem, I am loading files through the fileIO functions and these paths do not relate to include paths.

I was hoping there was some kind of global solution for both methods, (via an escape character). I managed to get the include references sorted now by using the method posted above.

I've got what I consider a temporary solution. by using chdir() before attempting to load the file. Is it normally done like this?

Nick.

Posted: Sun Dec 05, 2004 11:20 am
by rehfeld
can you use absolute file paths instead of relative?

Code: Select all

include($_SERVER['DOCUMENT_ROOT'] . '/path/to/script.php');

Posted: Sun Dec 05, 2004 11:26 am
by timvw
i usually have a auto_prepend file that only contains constants...

this way i can fopen(FRAMEWORK_WHATEVER_PATH . 'foo.bar');

Posted: Sun Dec 05, 2004 11:32 am
by jclarkkent2003
$DOCUMENT_ROOT is what you need.

It is the ROOT directory of that site, as in the /home/user/public_html/ for cpanel.

$_SERVER['DOCUMENT_ROOT'] problems

Posted: Sun Dec 05, 2004 12:09 pm
by firkinfedup
Hi there,

Sorry but $_SERVER['DOCUMENT_ROOT'] doesn't work for me, I get an undefined index error. I tried $DOCUMENT_ROOT index as well with no luck as well as $DOCUMENT_ROOT on it's own which didn't work.

What do you mean by "auto_prepend", I haven't heard of this before. I'll havea look at the manual. I'm sure what I'm doing is one of those things you learn from experience, I thought my site was working perfectly until I noticed the current directory changing with each page and breaking my relative links. Anyway, thanks loads for the replies, much appreciated, sorry for the delayed reply!

Nick.

Posted: Sun Dec 05, 2004 12:17 pm
by rehfeld
what version of php are u using?

im very suprised you dont have a document root variable available at all.

maybe try getnev('DOCUMENT_ROOT');


still, can you use absolute file paths?
you dont NEED the document root variable, its just easier

Posted: Sun Dec 05, 2004 12:32 pm
by firkinfedup
Hi there,

I'm using version 5.0.1

getnev didn't work for me, apparently it's an undefined function. Also dreamweaver didn't pick it up, but then again DW isn't the best of tools. But I'm definitely getting "undefined index" via the $_SERVER['DOCUMENT_ROOT'] method.

The way i've got it working at the minute is by changing the working directory to the root via a huge string, this to me isn't very good just incase the server gets modified etc and breaks my app. That's why I'm after a global variable method instead.

Posted: Sun Dec 05, 2004 1:03 pm
by kettle_drum
rehfeld made a little typo, try getenv()

Do:

Code: Select all

print_r($_SERVER);
to see what index values are available in that array.

Posted: Sun Dec 05, 2004 3:17 pm
by firkinfedup
Hi again,

Well I guess that solves that one, it appears I don't have a document root variable. Looking at the output of the printed array I can't see DOCUMENT_ROOT nor anything that looks like it produces the same value. Also getenv returned nothing at all.

Hmmm! :-\

Nick.

Posted: Sun Dec 05, 2004 4:36 pm
by timvw
Well, i don't like document root because it isn't always "correct". (btw, i just tested it on win32 and linux php5.0.2 and they both showed the document_root )

for example, if i'm user timvw -> http://foo/~timvw

document root will say: [DOCUMENT_ROOT] => /var/www

however, it is in /home/timvw/public_html.