Ben Phelps Posted November 5, 2006 Share Posted November 5, 2006 Is there a way to list the number of files in a defined directory. Link to comment https://forums.phpfreaks.com/topic/26186-list-of-files/ Share on other sites More sharing options...
Kelset Posted November 5, 2006 Share Posted November 5, 2006 I would look at this page http://ca.php.net/readdir to help you out. I'm a newb but by looking at the sample code on thatpage I would think it would look something like this$handle = opendir('/path/to/files')// loop over the directory. while (false !== ($file = readdir($handle))) { $file_array[].=$file }I think this would make an array of everything in the selected directory. Then all you would have to do ituse the count function on the array $file_num = count($file_array). I think that would give you the numberyou are looking for.Cheers!Stephen Link to comment https://forums.phpfreaks.com/topic/26186-list-of-files/#findComment-119774 Share on other sites More sharing options...
bljepp69 Posted November 5, 2006 Share Posted November 5, 2006 check out the glob(0 function - http://www.php.net/glob[code]foreach (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n";}[/code] Link to comment https://forums.phpfreaks.com/topic/26186-list-of-files/#findComment-119775 Share on other sites More sharing options...
JasonLewis Posted November 5, 2006 Share Posted November 5, 2006 with kelset's reply add in an if statement to check that the filename isn't '.' or '..' by going like this:[code=php:0]$dir = opendir('/dir/path');while(false !== ($file = readdir($dir))){if($file != "." && $file != ".."){$file_array[] .= $file;}}echo "Files in directory: ".count($file_array);[/code]kelset note that you didnt end your lines with ';'. Link to comment https://forums.phpfreaks.com/topic/26186-list-of-files/#findComment-119783 Share on other sites More sharing options...
Ben Phelps Posted November 5, 2006 Author Share Posted November 5, 2006 I found 1, thanks for the help. Kudos to all. [img]http://www.onecompare.com/images/emotions/smiley.gif[/img][code]<?phpfunction CountDir($aDir, $aRecurse){$Count = 0;$d = dir($aDir);while ($Entry = $d->Read()){if (!(($Entry == "..") || ($Entry == "."))){if (Is_Dir($aDir . '/' . $Entry)){if ($aRecurse){$Count += CountDir($aDir . '/' . $Entry, $aRecurse);}}else{$Count++;}}}return $Count;}echo CountDir(files/dir, True);?>[/code] Link to comment https://forums.phpfreaks.com/topic/26186-list-of-files/#findComment-119808 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.