php, display all folder just one level deeper

Associate
Joined
17 Sep 2006
Posts
36
Location
Thailand/UK
Why do it easily when you can make things really complicated? This took me yonks:





/******************************************************************************

Function: readD

Description: Used extensively to build array $arr of pages
$arr[x] is received and return becomes $arr[x][y]

Calls:

Called by:

Parameters: $dir=directory to be read
$mode="dir" finds directories
$mode="jpg" finds jpgs

Returns: array of directories found


******************************************************************************/
function readD($dir, $mode="dir"){
$dir.="/";
if(!is_dir($dir))return;
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
//echo "found $filename, $dir$filename<br>";
if($mode=="dir" && $filename!="." && $filename!=".." && is_dir($dir.$filename))$array[$filename] = "";
if($mode=="jpg" && ereg("jpg", $filename))$array[] = $filename;
}
return $array;
}
/******************************************************************************
* End of Function:
******************************************************************************/
 
Soldato
OP
Joined
12 Feb 2006
Posts
17,220
Location
Surrey
robmiller said:
Code:
foreach ( glob("./*", GLOB_ONLYDIR) as $dir ) {
    echo "<a href='$dir'>$dir</a><br />";
}

great quick script. 3 questions though.

1. Would you be so kind as to explain it to me, i kind of see whats happening here but don't understand whats going on.

2. Just realised that for every folder i have an upload folder where the miages go which i don't want to be displayed, is there something i can add that would then be like if( $dir == upload) { echo "";}.

3. Is it possible to get rid of the / before each folder? i was planning on using &raquo; and together they just look icky
 
Associate
Joined
5 Jan 2003
Posts
752
Location
Norfolk
addy_010 said:
great quick script. 3 questions though.

1. Would you be so kind as to explain it to me, i kind of see whats happening here but don't understand whats going on.

2. Just realised that for every folder i have an upload folder where the miages go which i don't want to be displayed, is there something i can add that would then be like if( $dir == upload) { echo "";}.

3. Is it possible to get rid of the / before each folder? i was planning on using &raquo; and together they just look icky

aye should be that assuming the dirrectory is actually named "upload"
Code:
foreach ( glob("./*", GLOB_ONLYDIR) as $dir ) {
    if($dir!="upload" || $dir!="." || $dir!=".."){
    echo "<a href='$dir'>$dir</a><br />";
   }
}

Not sure on what you mean with 3.
Are you trying to style the echo "<a href='$dir'>$dir</a><br />"; like <<CLICK>>
or do you mean the link to it (a href="$dir")?
 
Last edited:
Soldato
Joined
2 Dec 2005
Posts
5,515
Location
Herts
addy_010 said:
great quick script. 3 questions though.

1. Would you be so kind as to explain it to me, i kind of see whats happening here but don't understand whats going on.

2. Just realised that for every folder i have an upload folder where the miages go which i don't want to be displayed, is there something i can add that would then be like if( $dir == upload) { echo "";}.

3. Is it possible to get rid of the / before each folder? i was planning on using &raquo; and together they just look icky
The glob() grabs the directories and makes them an array. The foreach runs through this array and prroduces a link for each entry.

ChrisB's thing might do that for you.

Remove the / is easy. Something like
Code:
substr($dir, 1)
removes the first character from the entry I think?
 
Soldato
OP
Joined
12 Feb 2006
Posts
17,220
Location
Surrey
ChrisB said:
Code:
foreach ( glob("./*", GLOB_ONLYDIR) as $dir ) {
    if($dir!="upload" || $dir!="." || $dir!=".."){
    echo "<a href='$dir'>$dir</a><br />";
   }
}

Not sure on what you mean with 3.
Are you trying to style the echo "<a href='$dir'>$dir</a><br />"; like <<CLICK>>
or do you mean the link to it (a href="$dir")?

that new bit didn't work, im still being shown the folder called 'upload'.

what i meant by number 3 is that at the moment for the link im being shown ./Family but i want to jsut be shown Family as the link so that i can add >> before it.
 
Associate
Joined
5 Jan 2003
Posts
752
Location
Norfolk
addy_010 said:
that new bit didn't work, im still being shown the folder called 'upload'.

what i meant by number 3 is that at the moment for the link im being shown ./Family but i want to jsut be shown Family as the link so that i can add >> before it.

Odd... Mainly a perl man myself now, and don't dabble with php to much anymore :(
Code:
foreach ( glob("./*", GLOB_ONLYDIR) as $dir ) {
    if($dir=="upload" || $dir=="." || $dir==".."){
    }else{
      echo "<a href='$dir'>trim($dir, './')</a><br />";
    }
}
Updated i with jeNKs.
have a quick check of the upload case, as the == is case sensitive...
 
Soldato
OP
Joined
12 Feb 2006
Posts
17,220
Location
Surrey
ChrisB said:
Odd... Mainly a perl man myself now, and don't dabble with php to much anymore :(
Code:
foreach ( glob("./*", GLOB_ONLYDIR) as $dir ) {
    if($dir=="upload" || $dir=="." || $dir==".."){
    }else{
      echo "<a href='$dir'>trim($dir, './')</a><br />";
    }
}
Updated i with jeNKs.
have a quick check of the upload case, as the == is case sensitive...

ok one problem fixed, one still to go. I managed to not show upload folder by putting ==./upload instead of ==upload, but i still can't seem to get the ./ gone. I have tried trim but all that happens is that it echos "trim(./Family, './')". obviously problem is cos its in an echo bit so i tried it 4/5 other ways just before echo but it still iddn't work. Any clues?

have looked on php.net but there explanations i find to be the worst in the world. Not once have i read something on there site and understood it
 
Soldato
Joined
2 Dec 2005
Posts
5,515
Location
Herts
As you rightly guessed, it's taking trim to be part of the string, not it's own function.
Code:
foreach ( glob("./*", GLOB_ONLYDIR) as $dir ) {
    if($dir=="upload" || $dir=="." || $dir==".."){
    }else{
      echo "<a href='$dir'>" . trim($dir, './') . "</a><br />";
    }
}
Somebody will have a go at me for using the dots though I'm sure :D
 
Soldato
Joined
2 Dec 2005
Posts
5,515
Location
Herts
The echo deals with the bits in quotes as they are. The quotes " " specify the bit inside as a string, so it doesn't parse it for code. The trim() is a function so must be non-quoted. To fill the gap in between you use a dot.
 
Soldato
OP
Joined
12 Feb 2006
Posts
17,220
Location
Surrey
hehe guess what, i got another question that i'd appreciate help with..

After adding the code and seeing it all working thinking it would be good to leave a crumb trail (is that right saying) where it will display a link for the previous folders all the way to photos folder. for instance

photos >> Family >> Christmas >> 2007 >> Food

see how each is its own link and not just one big link.

thanks again all, you've been great help.


[edit] well after searching and searching the only stuff im coming up with is just long examples which dont work and with no explanation so im giving up. Anyone care to show me how to do this and explain it to me so i know how its donig what its doing?

thanks again

 
Last edited:
Soldato
OP
Joined
12 Feb 2006
Posts
17,220
Location
Surrey
just out of interest aswell, this site has 2 sections, one left, and one right. on the view images page the images are loaded on the left side of the page. If i put the same thing on another page will the image have to be loaded again or will they just stay in like the browser memory? same with anything aswell, when say the nav menu uses images, as it never changes do they get loaded on every page, or is it like first page you view loads it, and the browser doesn't get rid of it until you exit from the stie?
 
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
addy_010 said:
just out of interest aswell, this site has 2 sections, one left, and one right. on the view images page the images are loaded on the left side of the page. If i put the same thing on another page will the image have to be loaded again or will they just stay in like the browser memory? same with anything aswell, when say the nav menu uses images, as it never changes do they get loaded on every page, or is it like first page you view loads it, and the browser doesn't get rid of it until you exit from the stie?
most browsers cache images (and some even entire pages.)
 
Back
Top Bottom