On Tue, Apr 24, 2001 at 10:10:58AM -0400, Chad Day wrote:
> I'm trying to write a script to ftp the contents of one directory structure
> onto another server.. I'm having problems recursing the directories, either
> it a) doesn't do it or b) gets stuck in a loop.
> 
> Here's the function I'm having the problem with..
> 
> 

Directory recursion has angered me many long nights.  Here is what I
finally came up with, which works perfectly.  This should be easily
changed into what you need.. it's just an example of the logic. ;)

This was meant to run from the command line with /usr/bin/php -q ;)

$base = $argv[1];
$i = 0;
$directory[$i] = array ($argv[1]);

while (1) {
        if ( @ is_array ($directory[$i])) {
        $subdirs = "";
                foreach ($directory[$i] as $dir) {
                        if (($dir_handle = @ opendir ($dir))) {
                                print ("Reading $dir\n");
                                        while (($file = readdir ($dir_handle)) !== 
false) {
     // Drop these two, else we'll be no better
     // than glibc's glob() ;)
                                        if (($file != '.') && ($file != '..')) {
                                                if ( ! ereg ('^/', $dir)) {
                                                        $temp = $base . $dir;
                                                } else { $temp = $dir; }
                                                if ( ! ereg ('/$', $temp)) {
                                                        $temp .= '/' . $file;
                                                } else { $temp .= $file; }
     // Consider dropping symlinks right here,
     // or treating them as a normal file.
                                                if ( @ is_dir ($temp)) {
                                                        $subdirs .= $temp . ',';
                                                } else { print ("\t$file\n"); }
                                        }
                                }
                                closedir ($dir_handle);
                        }
                }
                $i++;
                if ($subdirs) {
                        $directory[$i] = explode (',', substr ($subdirs, 0, ((strlen 
($subdirs) - 1))));
                }
        }
        else { break; }
}

Hope that helps a bit.  I know that was one of the most frustrating things
I've ever tried to figure out (in any language).  It's certainly not
perfect.. you will probably need to add a few more string checks to it,
but the basic logic seems flawless to me.

Good luck! ;)


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to