RE: [PHP] Newbie: recursing directories

From: Date: Mon, 03 Jul 2000 21:35:34 +0000
Subject: RE: [PHP] Newbie: recursing directories
Groups: php.general 
Request: Send a blank email to php-general+get-4577@lists.php.net to get a copy of this message
> I have little bit of code that simply lists the contents of > directories; > However the code will only list the contents of the first directory > recursed. Do I have some kind of directory pointer problem? > Also, I cannot > get the "if ($file != "." && $file != "..") code to work at > all!!? which is > why I use readdir twice to bypass the . and .. entries. > > <?php > $dh=opendir("../doc"); > echo "Files:\n"; > $file = readdir($dh); > $file = readdir($dh); > while ($file = readdir($dh)) { > if (is_dir("../doc/$file")) { > echo "Directory $file<br>"; > readSubDir($file); > } > } > echo "FILE=$file"; > closedir($dh); > > ?> > <?php > function readSubDir($dirname){ > echo "Reading files for directory $dirname<br>"; > $sdh=opendir("../doc/$dirname"); > $sfile=readdir($sdh); > $sfile=readdir($sdh); > while ($sfile = readdir()) { > echo "SUBDIRFILE=$sfile<br>"; > } > echo "Closing...<br>"; > closedir($sdh); > > } > ?> Try this: function readSubDir($dirname){ echo "Reading files for directory $dirname<br>"; $sdh=opendir("../doc/$dirname"); //$sfile=readdir($sdh); //$sfile=readdir($sdh); while ($sfile = readdir()) { if( $sfile == '.' || $sfile == '..' ) continue; echo "SUBDIRFILE=$sfile<br>"; if( is_dir($sfile) ) readSubDir($sfile); } echo "Closing...<br>"; closedir($sdh); } -JOhn

« previous php.general (#4577) next »