problem referencing php

Soldato
Joined
19 Oct 2002
Posts
3,480
hi guys...

i'm trying to reference some functions in fns_all.php (in the root directory) that output things like the standard html header and the like...

the file doing the referencing is in a folder a few levels deep, and at the top of that file i have used:

include('/fns_all.php');

which should just look for that file in the root directory should it not under standard absolute referencing rules?

its got me foxed cuz the page is not getting access to the fns_all file... any ideas?

(doesn't work on my local MAMP setup or my VDS)
 
Last edited:
gonna tag onto this:

in my index.php... i also have include('fns_all.php'); - however, if i add a forward slash to the front of that, it also messes it all up (just end up with a blank page)
 
the slash doesn't work the way you think it does - root level to php would probably be one or two directories up from your public_html or www directories (or whatever you changed it to if it's different). I believe it references apache's root directory. Try playing around with ../ to make you go "up" a directory
 
hmm ok i see...

so php actually shuts down the option of placing files wherever you want doesn't it, you have to know exactly how deep each file is so you can add the right number of "../"s

thats a shame that is...
 
like I said before, / just references a directory that you wouldn't necessarily think it would. What I do is set a configuration variable called __DOCROOT__. To get the value of __DOCROOT__, echo out $_SERVER['DOCUMENT_ROOT'] and put that in there (as a point of preference, I add a / on the end as well). This makes your app more portable.

It's also really worth thinking about your file structure. I can't think of an instance where I'd be in a file and not know the location of all my other files, so definitely bear that in mind.
 
I believe it references apache's root directory.

It's the filesystem root, actually. require/include/any other PHP function just take regular filenames, so you can do:

Code:
include '/etc/foo/bar.txt';
echo file_get_contents('/var/www/foo/bar/baz.gif');
// etc

To get Apache's document root you need to use $_SERVER['DOCUMENT_ROOT'].
 
Ah, I didn't know that. Like I said, I normally just set my own up for portability but that's worth knowing.
 
you can do:
Code:
include '/etc/foo/bar.txt';
echo file_get_contents('/var/www/foo/bar/baz.gif');
// etc

i'm a little lost with this as i'm trying to do an include like that but it doesn't work; links work by using the / in front but for me to start at the document root i need to use $_SERVER['DOCUMENT_ROOT']. How can i get it so i don't have to use $_SERVER['DOCUMENT_ROOT'] to get start from the root directory of my files, so where index.php is found?

atm i have all my pages set up to include_once start.php which is where ever
$_SERVER['DOCUMENT_ROOT'] is, then define $rootPath as whatever is needed to get to the root of my files. this then means whenver i craete a new page thats deaper then the root of my files i odn't need to redefine the location for all the includes, and also if the location of $_SERVER['DOCUMENT_ROOT'] ever changes i just need to chage where start.php is and then change $rootPath to whatever the new path is and everything will work fine.

It's an ok solution for now but it doesn't work correctly when on a server, i haven't looked into it but imagine it will be because i might not be able to put start.php where
$_SERVER['DOCUMENT_ROOT'] loads to.

Because of this i would prefer a better solution if possible but i tried your suggestion (as well as other suggestions) but it doesn't work.
 
Last edited:
Back
Top Bottom