note 41432 added to function.readdir

From: Date: Fri, 09 Apr 2004 20:16:37 +0000
Subject: note 41432 added to function.readdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-67923@lists.php.net to get a copy of this message
I often need to process all the files in a given folder (e.g. processing XML files for a web display). This function returns an array giving the paths to all the files in a given folder. I hope someone finds it as useful as I do :) Doing <?php count(dirListAllFiles('./someFolder')) ?> will return the number of files found of course ;) <?php print '<pre>'; function dirListAllFiles($path) { $filesPaths = array(); if ($dirHandle = opendir($path)) { while (false !== ($file = readdir($dirHandle))) { if (is_dir($path . '\\\\' . $file)) { // If it is a valid directory add the files in that directory // to the list (and that will already have all the files in that // due to recursion if ($file != '.' && $file != '..') $filePaths = array_merge($filePaths, dirListAllFiles($path . '\\\\' . $file)); } else $filePaths[] = $path . '\\\\' . $file; } closedir($dirHandle); } return $filePaths; } print_r(dirListAllFiles('./myFolderWithLotsOfFiles')); print '</pre>'; ?> ---- Manual Page -- http://www.php.net/manual/en/function.readdir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41432 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41432&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41432&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67923) next »