PHP – Getting Files, Folders, File Count, Folder Count in a Directory

This following PHP example helps you to getting the files, folders, file count, folder count in a
directory.

Code Example:

<?php
$folderCount = $fileCount = 0;
//. means current directory
//if you wanna a learn a folders inside use opendir('path')
if ($handle = opendir('.')) {
	while (false !== ($entry = readdir($handle))) {
		if ($entry != "." && $entry != "..") {
			if (is_dir($entry)) {
				echo "Folder => " . $entry . "<br />";
				$folderCount++;
			}
			else {
				echo "File => " . $entry . "<br />";
				$fileCount++;
			}
		}
	}
	
echo "<br />--------------------------------------<br />";
echo "Total Folder Count : " . $folderCount . "<br />";
echo "Total File Count : " . $fileCount;
closedir($handle);
}
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail