List only the files from folders

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

Post Reply
mih
Forum Newbie
Posts: 12
Joined: Tue Jul 28, 2009 12:32 am

List only the files from folders

Post by mih »

Hello,

The structure is for example:

Sellers
---test1
--------new folder
--------orders
--------file1.txt
--------file2.txt
--------file3.txt
---test2
--------file1.txt
---index.php
---list.php

list.php is

Code: Select all

<?php
 
 $dir = ".";
 $bad = array(".", "..");
 
  $contents = scandir($dir);
  $folders  = array_diff($contents, $bad);
  
   foreach (array_values($folders) as $foldername) {
    
    if (is_dir($foldername)) {
        echo "Folder: $foldername<br />\n";
            
        $subfolder = scandir("$foldername");
        $files = array_diff($subfolder, $bad);
             
           foreach (array_values($files) as $filename) {
              var_dump(is_dir($filename)); 
                 echo "Filename: $filename<br />\n";
               }
               
      echo '<br>';
        }   
    }
//print_r($files);
?>
and the result:

Code: Select all

Folder: test1
bool(false) Filename: new folder
bool(false) Filename: list_1260506826.txt
bool(false) Filename: list_1260511368.txt
bool(false) Filename: list_1260553280.txt
bool(false) Filename: orders
 
Folder: test2
bool(false) Filename: DB20091203211632.txt
Why are 'new folder' and 'orders' not considered folders ?

I used var_dump to print the check, if I'd used the condition

Code: Select all

if(!is_dir...
I would have got everything.

Thank you
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: List only the files from folders

Post by requinix »

Hint: notice how $filename is "new folder", not "test1/new folder".
Last edited by requinix on Sat Dec 12, 2009 5:09 pm, edited 1 time in total.
mih
Forum Newbie
Posts: 12
Joined: Tue Jul 28, 2009 12:32 am

Re: List only the files from folders

Post by mih »

Thanks again, lots of grievance saved.

I did

Code: Select all

is_dir($foldername.'/'.$filename)
Post Reply