Getting Folder Content

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Getting Folder Content

Post 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?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

maybe try an if statement? Like if( $folderEntry != '.' ).....
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

Yes, that would be obvious but I thought you can't use that in the WHILE statement?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post 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.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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();

?>
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What do you mean by "max column"?
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's the exact same concept as data from a database.
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you using the example I posted that's linked from Useful Posts? (The first two links should be of interest.)
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post 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;
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

could it be with the for loop in the while statement?
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

I'm getting lost here,
now it shows shows the first picture in a formatted table :(
Post Reply