TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-26-2009, 12:46 PM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default Glob recursion not working right

I need to be able to read through all directories in a given path and list all of the files. I'm using the following code and it works for the top level but not sub-directories. For example, if there is this structure
Code:
dirA
 dirB
  fileB 
 fileA
If will enter into dirA and display fileA. It will also show that it is going into the sub-directory dirB but fileB is never listed. Would someone please point out what I'm missing or let me know if there is a better way to do this?

PHP Code:
function GetAllFiles(&$files$locn)
{
  
$dirs glob($locn '/*'GLOB_ONLYDIR);
   
  if(
is_array($dirs) && count($dirs) > 0)
  {
    foreach (
$dirs as $dir)
    {
      
$cwd getcwd();
      
chdir ($dir);      
      
      foreach (
glob("*") as $filename
      {
         if (
is_dir($filename))
         {
           echo 
'enter subdir '.$filename.'<br>';
           
GetAllFiles($files$dir '/'.$filename);
         }  
               
         echo 
'file '.$filename .'<br>';
         
$files[] = $filename;
      }
       
chdir ($cwd); 
    }
  }

benton is offline  
Reply With Quote
Old 09-26-2009, 01:18 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

It seems a bit of a convoluted method of traversing the directories, especially with changing the current working directory so often. You could change the call to the second glob (in the foreach) to use full (or relative) paths rather than just *. Or you could use the handy RecursiveDirectoryIterator which makes life easier.
Salathe is offline  
Reply With Quote
Old 09-26-2009, 03:06 PM   #3 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

I'm not sure I understand how to make that change but I tried the following. The result is the same though. The sub-directories are listed but not their contents. This is actually the code I started with but when it didn't work, I added the recursive call to try to get in to the sub-directory.

As for RecursiveDirectoryIterator, I used that first and it did work in some cases. But besides it requiring php 5, the main problem is that it was causing an out of memory condition. That may have been due to the part of the code not shown where the contents of the files are read, but I decided to make it php 4 compatible and switched to the glob function.
PHP Code:
function GetAllFiles(&$files$locn

  
$dirs glob($locn '/*'GLOB_ONLYDIR); 
    
  if(
is_array($dirs) && count($dirs) > 0
  { 
    foreach (
$dirs as $dir
    { 
      foreach (
glob($dir '/*') as $filename)  
      { 
         echo 
'file '.$filename .'<br>'
         
$files[] = $filename
      } 
    } 
  } 

benton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Categories and sub categories - recursion Dinky Advanced PHP Programming 2 02-01-2010 07:45 PM
Wamp working, but what next? Help! DotNetTim Absolute Beginners 3 02-07-2009 10:05 AM
Mod Rewrite only partially working Gareth General 6 12-24-2008 06:08 PM
Traverse Directories the Easy Way with Glob() ! Wildhoney Absolute Beginners 6 12-02-2008 11:07 PM
Exec(zip) in PHP not working as expected oscargodson General 3 09-28-2008 12:00 PM


All times are GMT. The time now is 09:52 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design