absolute paths

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
do I have to prefix every absolute path with document root to get it to  

For some reason I thought that prefixing a path with '/' or './' with make  
it absolute w.r.t to document root but I guess not?

e.g., when I do

include './Scripts/AddNav.php';


include '/Scripts/AddNav.php';

It only happens to work if that path is in the current directory but it  
won't goto the root directory.

i.e., the above is doing the exact same as if I did

include 'Scripts/AddNav.php';

But in any case this doesn't work in general unless I use

include $_SERVER['DOCUMENT_ROOT'].'/Scripts/AddNav.php';

ofcourse this seems like a mess to do every time I want to use an absolute  
path... which is a lot.

Is that what I'm stuck with doing or is there a function, say, like abp that  
will take a path and prefix the document root to it?


Re: absolute paths

Quoted text here. Click to load it

An absolute *FILE* path or absolute *URL* path?  There are important

If you are attempting to use a URL path as an absolute file path,
yes, you have to prefix it with document root.

A file path might be used like this (on UNIX; try DIR on Windows):

    ls -l /usr/local/www/document_root/images/flag.jpg

A URL path might be used like this:


Quoted text here. Click to load it

If you mean something like:

just prefixing it with / makes it an absolute URL.  To get a *FILE* path,
you need to prefix the document root.

If you prefix "./", you're making it relative (to *WHAT* depends on
context - for include, see include_path).

Quoted text here. Click to load it

include takes a *FILE* path.

Quoted text here. Click to load it

If you've got an include file that you include at the front of every
page, (PHP autoinclude?) you could include that one as above, and
inside it, set $ScriptDir and then:

include $ScriptDir.'/AddNav.php';

which is a little shorter.

Re: absolute paths

Quoted text here. Click to load it

No, only file paths... shit... I guess ;/ Didn't realize there was a  

When someone uses my site: www.jonslaughter.com/somedir/somefile.php, can  
everything after the domain name be considered a file path? (atleast if it  
actually looks like a file path)

That is, on my site I will be using file paths to represent url paths.  
Theres a one to one correspondence between the urls and files. (excluding  
the additional domain name and protocol in the url)

Quoted text here. Click to load it

Ok, but what I am doing is only keeping the /images/flag.jpg

so maybe I'll have a file in document_root that opens the flag.jpg

if I do something like read('/images/flag.jpg') then it works because it  
uses the relative dir scheme. But now if I wasn't in document root then it  
wouldn't(assuming there is no /images/flag.jpg in that dir)

Quoted text here. Click to load it

ok.  I guess thats it then.  I thought then that you could use absolute urls  
and the would be resolved w.r.t to the document root.

Quoted text here. Click to load it


So essentially what your saying is that there is no such thing as absolute  
file paths?  That is, all file paths are relative?

Quoted text here. Click to load it

YEah, I thought about that. I haven't got the autoinclude stuff to work but  
was just going to create a function like ap(somepath) that would prefix  
somepath with the document root.

I just needed to understand the difference.  I didn't realize that there  
were url and file paths and that only urls had the absolute ability. I think  
I got it now though.


Re: absolute paths

Jon Slaughter wrote:
Quoted text here. Click to load it


If it's being accessed using http protocol (i.e.  
http://www.example.com/myfile.php ) it is a URI and relative to your  
document root.

If it's being accessed from your php code, i.e. by include (_once),  
require (_once), fopen (to the local filesystem) it's relative to the  
root directory of your machine.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: absolute paths

Quoted text here. Click to load it

Ok, thanks. I didn't know there was a differentiation.  

Re: absolute paths

Quoted text here. Click to load it

My bet is that the document root of your web site is NOT / .
And it certainly shouldn't be.

Quoted text here. Click to load it

No, there isn't.  There should be *NO* URL that pulls up the file
/etc/passwd from your system.  Or in the case of Windows, there should
be *NO* url for the files containing the registry, or any of the files
in the Windows system directory.  There should be *NO* URL that pulls up
most of the executables for your system, as they might be copyrighted
and your license may not permit you to distribute them without the
corresponding source code.  Besides, nasty people will see you have
unpatched executables and try to crack your system.

Your URL might be: http://www.jonslaughter.com/images/flag.jpeg
The corresponding file might be:

Quoted text here. Click to load it

Files do not open other files.

Quoted text here. Click to load it

To further confuse things, there is the PHP setting include_dir,
and PHP does have a current working directory, which you can change.
I suspect a lot of what you claim about how things work will suddenly
break if these are changed.

On my system, I have an include directory which is *OUTSIDE* the
document root, for, among other things, database login and password
information.  This directory is listed, by absolute FILE path, in
include_path.  I can refer to a particular file containing a
particular database login for a particular application with:
    include "tv.inc";
from *ANY* PHP script on any of several virtual hosts, in one of
the document root directories or several levels below that, and
I'll always get the file.  The only caveat is that I can't leave
a different "tv.inc" file around in the same directory as one of
the scripts.  Even that I could fix by putting the path to my include
directory *FIRST* in include_path, or leave "." out of it entirely.

Quoted text here. Click to load it

You can use an absolute URL *IN A BROWSER* and they will be resolved with
respect to the document root.  That is *NOT* true of "include".

Quoted text here. Click to load it

No, there are absolute file paths.  File paths are what you use
with a shell, and what you get back when you type "pwd".  There
won't be any URL paths to files on your system if you remove the
web server (and therefore, PHP).

Look at it this way:  You have to use a file path where a file path
is required and you have to use a URL path where a URL path is
required.  In some places you can use either but a URL path has to
start with "http:".  You can't mix them, just like you cannot dial
a postal address nor can you put a phone number and a stamp on a
letter and expect it to arrive at the correct place.  It may be
that in your office complex, your office number is both the last 3
digits of your phone number AND it's the last 3 digits of your (USA,
9-digit) zip code.  You can use a short-cut reference (relative
path) to your office by just office number, but not when talking
to someone in a different building.

Quoted text here. Click to load it

Fine.  Now how do you define that function?  With include, or require,
probably.  Otherwise it's a lot more work than  
include $_SERVER['DOCUMENT_ROOT'].'/Scripts/AddNav.php';

Quoted text here. Click to load it

Re: absolute paths

On Apr 22, 1:24 am, gordonb.n5...@burditt.org (Gordon Burditt) wrote:
Quoted text here. Click to load it

I always did something like this.. I Can't remember the exact $_Server
part b/c I am not at home, but it goes something like that where
$_Server is http://www.orbstra.com/ and then I toss guava/ at the end.

class get
      function url()
             return ($_SERVER['URL'] . guava/);

so when I need to:

include get::url() . 'system/';

So on and so forth...

Site Timeline