Forum Moderators: coopster

Message Too Old, No Replies

link to ../

back one folder

         

WhosAWhata

10:20 pm on May 24, 2004 (gmt 0)

10+ Year Member



if you are in a folder
site.com/some/random/ammount/of/folders/

how do you make a script link back to
site.com/some/random/ammount/of/

without being thrown off by (note no trailing slash)
site.com/some/random/ammount/of/folders

my loop looks like this
$dir="dir/";
$d=dir($dir);
while($f=$d->read()) {
if($f == "..") {
//change link
} else {
//other stuff
}
}
$d->close();

WhosAWhata

11:39 pm on May 25, 2004 (gmt 0)

10+ Year Member



anyideas? i'm sure it could be done with Preg_replace but a don't know the syntax...
right now my code looks like this (sort of)

<?
if (!$dir) $dir = "dir/";
$d = dir($dir);
while($entry = $d->read()) {
if($entry!= ".") {
if(is_dir($dir.$entry)){
$r[] = "<a href=\"".$PHP_SELF."?dir=".$dir."/".$entry."/\">".$entry."</a>";
}else{
$f[] = "<a href=\"".$dir.$entry."\">".$entry."</a>";
}
}
}
natcasesort($r);
natcasesort($f);
$html = "<h2>folders</h2>".implode("<br>",$r)."<hr><h2>files</h2>".implode("<br>",$f);
echo $html;
?>

so if you click ../ your url ends up looking like this
[site.com...]

when it could be simplified to
[site.com...]

stuartc1

11:20 am on May 26, 2004 (gmt 0)

10+ Year Member



An easy way to do this is to pass the full path (from the root of the dirs you are browsing) in all entries :) I like doing things the easy way!

However if there are many many folders then this is probably not practicle. There are many ways to do this kind of thing, but without much of your code it's hard to advise.

regex may well prove to be the best way.

py9jmas

12:07 pm on May 26, 2004 (gmt 0)

10+ Year Member



Is there a reason why you can't just use
<a href="../">Blah</a>?

Or am I not understanding this right?

WhosAWhata

9:32 pm on May 26, 2004 (gmt 0)

10+ Year Member



i am using <a href="files.php?dir=WHATEVER/THE/DIR/WAS/../">../</a>

but this eventually causes a long url
like
files.php?dir=dir/subdir/../anotherSubdir/dirInsideSubdir/../../subdir/

which is equal to

files.php?dir=dir/subdir/

httpwebwitch

2:29 pm on May 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Split your URL into parts separated by "/"
Then step through the array. When you encounter ".." as a step in the path, pop off the previous directory, and continue.


$path = explode("/",$URL);
$pathcopy = array();
foreach($path as $k=>$v){
if ($v==".."){
array_pop($pathcopy);
}else{
array_push($pathcopy,$v);
}
}
$URL=implode("/",$pathcopy);

WhosAWhata

10:01 pm on May 27, 2004 (gmt 0)

10+ Year Member



perfect...thanks