Site root and constantly changing working directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

Site root and constantly changing working directory

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

time to discover what php's include_path is.
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

Works for includes

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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");
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

Hi there

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

can you use absolute file paths instead of relative?

Code: Select all

include($_SERVER['DOCUMENT_ROOT'] . '/path/to/script.php');
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i usually have a auto_prepend file that only contains constants...

this way i can fopen(FRAMEWORK_WHATEVER_PATH . 'foo.bar');
jclarkkent2003
Forum Contributor
Posts: 123
Joined: Sat Dec 04, 2004 9:14 pm

Post 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.
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

$_SERVER['DOCUMENT_ROOT'] problems

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
firkinfedup
Forum Newbie
Posts: 19
Joined: Sat Dec 04, 2004 7:25 am

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
Post Reply