Page 1 of 2

Getting Folder Content

Posted: Tue Mar 13, 2007 7:21 am
by thiscatis
I'm currently using this code to get the content of a folder.

Code: Select all

<?php
$folder=dir(".");

while($folderEntry=$folder->read()){
      echo $folderEntry."<br>";
}

$folder->close();

?>
But it also shows the
.
..
Is there a way to stop those two lines from the output?

Posted: Tue Mar 13, 2007 7:24 am
by Begby
maybe try an if statement? Like if( $folderEntry != '.' ).....

Posted: Tue Mar 13, 2007 7:26 am
by thiscatis
Yes, that would be obvious but I thought you can't use that in the WHILE statement?

Posted: Tue Mar 13, 2007 7:29 am
by Begby
thiscatis wrote:Yes, that would be obvious but I thought you can't use that in the WHILE statement?
Perhaps you should try it.

Posted: Tue Mar 13, 2007 7:34 am
by thiscatis
Sorry dude,
I tried it some time ago but I used if(!$folderEntry = '.' ),
used if( $folderEntry != '.' ) now and another if clause with if( $folderEntry != '..' )
Works like a charm,
Thanks a lot mate.

Posted: Tue Mar 13, 2007 7:51 am
by Begby
Excellent!

You can do it like this too instead of using two statements

Code: Select all

<?php
$folder=dir(".");

while($folderEntry=$folder->read()){
  if( $folderEntey != '.' && $folderEntry != '..' ) {
    echo $folderEntry."<br>";
  }
}

$folder->close();

?>

Posted: Tue Mar 13, 2007 8:37 am
by thiscatis
Is there any way to get this content in a formatted table with a max column?
I had a look with a for loop but didn't get the expected results :s

Posted: Tue Mar 13, 2007 8:50 am
by feyd
What do you mean by "max column"?

Posted: Tue Mar 13, 2007 9:23 am
by thiscatis
Instead of getting a list with filenames I would like to have it in a table with a maximum of for e.g. 8 colums
I know how to do this for content from a database but not from files in a folder.

Posted: Tue Mar 13, 2007 9:26 am
by feyd
It's the exact same concept as data from a database.

Posted: Tue Mar 13, 2007 9:51 am
by thiscatis
I tried to put the for loop with a

Code: Select all

$total_images = count(glob("/MY PATH IS HERE/{*.JPG,*.jpg,*.png}", GLOB_BRACE));
for a file count in the WHILE loop but it keeps looping and not showing any correct tables?

Posted: Tue Mar 13, 2007 9:57 am
by feyd
Are you using the example I posted that's linked from Useful Posts? (The first two links should be of interest.)

Posted: Tue Mar 13, 2007 10:49 am
by thiscatis
Hey feyd,
yes those are the ones i'm using,
my code:

Code: Select all

//$s_pics is declared before this snippet

$folder=dir("modules/shoots/$s_pics/");

   while($folderEntry=$folder->read()){
  
        if( $folderEntry != '.' ) 
     { 
	 	if( $folderEntry != '..' ) { 
		
		   $output .= "<table cellpadding=8 width=100%>\n\n";
                   $howmany = $count(glob("/MY PATH IS HERE/$s_pics/{*.JPG,*.jpg,*.png}", GLOB_BRACE));; //added dummy path
                   $rowmax = 3;
                     
                    for($x = 0; $row = $folderEntry; $x++)
                       {
                if($x % $rowmax == 0)
                   $output .= "<tr>\n";
                   $output .= "<td style=\"width: 33%\"><img src=modules/shoots/thumbsup.php?image=$s_pics/$folderEntry&width=225 /></td>";
               
               if($x % $rowmax == $rowmax - 1)
                   $output .= "\r</tr>\n\n";
         }

                if($left = (($howmany + $rowmax - 1) % $rowmax))
                    $output .= '<td colspan="' . $left . '">' . "</td>\n</tr>\n\n";
                    $output .= "</table>\n\n";

echo $output;

Posted: Tue Mar 13, 2007 12:26 pm
by thiscatis
could it be with the for loop in the while statement?

Posted: Tue Mar 13, 2007 4:49 pm
by thiscatis
I'm getting lost here,
now it shows shows the first picture in a formatted table :(