Check if specific folder contains files, folders, both or no

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
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Check if specific folder contains files, folders, both or no

Post by Peuplarchie »

Good day to you all,
is there a way to check if a specific folder contains: files, folder, both or none ?

I read my folder with Glob, like my code here :

Code: Select all

 
 
<?PHP
function globDir($dir)
{
   $files = glob("$dir/*", GLOB_ONLYDIR);
   
     if(!empty($files))
   {
      echo "<ul>\n";
 
      foreach($files as $file)
      {
         echo "<li><b>". basename($file)."</b>\n";
         globDir($file);
         echo "</li>\n";
      }
      echo "</ul>\n";
   }
}
globDir('Photos');
?>
 
 

The only thing I have really found is how to find if a specific file exists.

I'm trying to find out if a specific folder contains : files, folders, both, none.
It would return the appropriate word accordingly.

Thanks and take care!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Check if specific folder contains files, folders, both or no

Post by s.dot »

1. Check folders to see if they have contents besides '.' and '..'.
1.a See example #2 here.

2. While looping, use is_dir() and is_file() to check if the contents are folders or files.
2.a is_dir() and is_file() can act screwy on windows.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply