mapping relative path to absoulte path

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

kunal_it
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Oct 15, 2002 10:26 am
Location: Raleigh
Contact:

mapping relative path to absoulte path

Postby kunal_it » Wed Oct 16, 2002 10:34 am

is there a way to get the absolute path from a relative path such as "../../mydir/" which lies under the document root ?

Thanks,
Kunal

DoppyNL

Postby DoppyNL » Wed Oct 16, 2002 11:16 am

I don't know how to convert a relative path wich got ".." in it to an absolute path, but you could write some code that can do it or do it by hand.
The absolute path to the current script can be found in $_SERVER['DOCUMENT_ROOT']

I only can't seem to find out why you want to convert it... They both work...

Greetz Daan

kunal_it
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Oct 15, 2002 10:26 am
Location: Raleigh
Contact:

Postby kunal_it » Wed Oct 16, 2002 11:22 am

the reason y i want to convert a path with ".." is that I am sure that the relative path isn't going to change..however, there might be some changes if I use absoulte path using document root..

for eg..

say i m in

/dir1/dir2/dir3/test.php, where dir1 is in the doc. root..

now, its possible that later on, the directory structure may look like

/new_dir/dir1/dir2/dir3/test.php, in which case the paths that I may be using relative to document root may not hold valid.

however, if I use a path relative to test.php, then I am not affected by directory structure changes outside dir1..

so, ultimately, I want to some how get the absolute path using the relative path.

hope that clarifies the purpose.

thanks,
Kunal

Jay

Postby Jay » Wed Oct 16, 2002 1:16 pm

There is a way, but it's not 100% bullet proof! You can take the full document root path of the file you're in, and traverse up and down the levels based on the relative path to find it's absolute path.

What you'd need to do is explode it using / so it splits up all the directories. In the relative path, see how many directories it travels up, and delete that many dirs (remove the file first) off the end of the array, then append the rest of the URL.

This is actually what I do with my sites. The first bit works out where the file is in relation to the site, and sets up session variables. Each and every other script can call any other one by working out where they are. I can change my server OS, or path pattern and it'll still work!


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron