View Full Version : Going back directories

04-09-2003, 09:53 PM
Is there a way in PHP to work out how many ../../'s the page is from the root directory of the site ?

For example if I have an included txt file on all my pages, and some pages are at different directory levels, and there is a link like 'blah/blah.php' in the txt file, if someone on a page like mysite.com/xyz/abc/123.php were to click it, it would try and go to mysite.com/xyz/abc/blah/blah.php.

So I want it to print the correct number of dots and forward slashes before the url, so it would be ../../blah/blah.php on the 123.php page.

Hope that doesn't sound too complicated :)

04-09-2003, 11:59 PM
links from any included file will be relative to the heirarchical top document.

/index.php includes folder/folder/a.php which links to /dir/dir/b.php

that will link to /dir/dir/b.php from where index.php is.

04-10-2003, 12:20 AM
Sorry i'm not good at explaining..

x/y/x/indexA.php includes global/xyz.txt which has the link 'b/v/c/indexB.php' in it.

If the user clicks that link on indexA.php then it will try to go to x/y/z/b/v/c/indexB.php, because the ../../../ are not before the url link.

If it was ../../../b/v/c/indexB.php then it would go to b/v/c/indexB.php.

I would like to know, how using PHP can it work out how many '../' to put before the url in the link to indexB.php so that it will go straight to it.

04-10-2003, 07:57 AM
If you're on linux, you can just put the full url in the includes. It won't work on a windows machine though

04-10-2003, 09:31 AM
Nightfire obviously I know I could put the full URL but I don't want to :)

04-10-2003, 09:36 AM
at a dodgy guess (ie play around with until this works)

$num_dirs = count(explode('/',$_SERVER['REQUEST_URI']))-2;

really you should not need workaround scripts like that though - perhaps taking a few hours to sort out your directory structure would be time better spent.

04-10-2003, 10:47 AM
Thanks Okii.

I have this so far:

$num_dirs = count(explode('/',$_SERVER['REQUEST_URI']))-2;
if ($num_dirs == "1") { $dots = "../"; };
if ($num_dirs == "2") { $dots = "../../"; };
if ($num_dirs == "3") { $dots = "../../../"; };
if ($num_dirs == "4") { $dots = "../../../../"; };
if ($num_dirs == "5") { $dots = "../../../../../"; };
if ($num_dirs == "6") { $dots = "../../../../../../"; };
if ($num_dirs == "7") { $dots = "../../../../../../../"; };
if ($num_dirs == "8") { $dots = "../../../../../../../../"; };
if ($num_dirs == "9") { $dots = "../../../../../../../../../"; };
if ($num_dirs == "10") { $dots = "../../../../../../../../../../"; };
echo "<a href=\"$dots index.php\">index</a>";

Isn't there is a simpler way of doing that instead of having 10 'ifs' ?

Also I can't figure out how to get the dots and slashes to join with the index.php in the echo :confused: I tried $dotsindex.php but it doesn't work.

04-10-2003, 05:35 PM
This basically does the same ( i think :D ) but it uses a loop, so you could loop through several times, more or less than 10.

$num_dirs = count(explode('/',$_SERVER['REQUEST_URI']))-2;
$dot = '../';

for($i = 1; $i < 11; $i++)

for($x = 0; $i < $i; $x++)
$da .= $dot;

eval('if($num_dirs == "'.$i.'") { $dots = '.$da.'; }');


echo "<a href=\"$dots index.php\">index</a>";

Hope this helps :thumbsup:

04-10-2003, 05:49 PM
the number of ../'s always reflects $num_dir, so

$dots = '';
for($a=0; $a < $num_dirs; $a++)

would be all you'd need.


echo '<a href="' .$dots. 'index.php">........';

04-10-2003, 07:44 PM
lol! I cant believe I missed that! That seems so simple now! Thanks Okii! :D