How can I count all files in a directory with PHP? (e.g. 124 Files)
I'm trying to figure out how I can count all of my XML files in one directory. How ca开发者_StackOverflow社区n I do this with PHP?
$dir = "random_directory/";
$files = glob("$dir*.xml");
$count = $files !== false ? count($files) : 0;
echo $count;
Just change $dir to your directory.
you can use glob
$count = 0;
foreach (glob("*.xml") as $filename) {
echo $filename; //gives you the file name
$count++;
}
echo $count;
or per MitMaro, simply just get the # of xml files without accessing file information:
echo count(glob("*.xml"));
$files = scandir('/path/to/dir/');
$count = 0;
foreach ($files as $file) {
if (strtolower(substr($file, 4)) == '.xml') {
$count++;
}
}
http://php.net/scandir
http://php.net/substr
精彩评论