Site root and constantly changing working directory
Moderator: General Moderators
-
firkinfedup
- Forum Newbie
- Posts: 19
- Joined: Sat Dec 04, 2004 7:25 am
Site root and constantly changing working directory
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.
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.
-
firkinfedup
- Forum Newbie
- Posts: 19
- Joined: Sat Dec 04, 2004 7:25 am
Works for includes
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
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
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
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.
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.
can you use absolute file paths instead of relative?
Code: Select all
include($_SERVER['DOCUMENT_ROOT'] . '/path/to/script.php');-
jclarkkent2003
- Forum Contributor
- Posts: 123
- Joined: Sat Dec 04, 2004 9:14 pm
-
firkinfedup
- Forum Newbie
- Posts: 19
- Joined: Sat Dec 04, 2004 7:25 am
$_SERVER['DOCUMENT_ROOT'] problems
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.
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.
-
firkinfedup
- Forum Newbie
- Posts: 19
- Joined: Sat Dec 04, 2004 7:25 am
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.
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
rehfeld made a little typo, try getenv()
Do:
to see what index values are available in that array.
Do:
Code: Select all
print_r($_SERVER);-
firkinfedup
- Forum Newbie
- Posts: 19
- Joined: Sat Dec 04, 2004 7:25 am
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.
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.