Reply to Re: absolute paths

Your name:

Reply:


Posted by Gordon Burditt on 04/22/07 05:24

>> >do I have to prefix every absolute path with document root to get it to
>>>work?
>>
>> An absolute *FILE* path or absolute *URL* path? There are important
>> differences.
>
>No, only file paths... shit... I guess ;/ Didn't realize there was a
>difference?
>
>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.

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

>Theres a one to one correspondence between the urls and files. (excluding
>the additional domain name and protocol in the url)

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:
/usr/local/www/document_root/images/flag.jpeg

>> 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:
>>
>> http://myhost.mydomain.com/images/flag.jpg
>>
>>
>
>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

Files do not open other files.

>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)

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.

>>>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?
>>
>> If you mean something like:
>>
>> SRC="/images/flag.jpg"
>> just prefixing it with / makes it an absolute URL. To get a *FILE* path,
>> you need to prefix the document root.
>>
>
>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.

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".

>> If you prefix "./", you're making it relative (to *WHAT* depends on
>> context - for include, see include_path).
>>
>>>
>>>e.g., when I do
>>>
>>>include './Scripts/AddNav.php';
>>>
>>>or
>>>
>>>include '/Scripts/AddNav.php';
>>
>> include takes a *FILE* path.
>
>ok.
>
>So essentially what your saying is that there is no such thing as absolute
>file paths? That is, all file paths are relative?

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.


>>>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?
>>
>> 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.
>>
>
>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.

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';

>
>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.
>
>Thanks,
>Jon
>
>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация