Warning: include(./include.php) [function.include]: failed to open stream: No such file or directory in /home/stree31/public_html/Bartimus/count_files_in_folder.php on line 1

Warning: include(./include.php) [function.include]: failed to open stream: No such file or directory in /home/stree31/public_html/Bartimus/count_files_in_folder.php on line 1

Warning: include() [function.include]: Failed opening './include.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/stree31/public_html/Bartimus/count_files_in_folder.php on line 1
Count number of files in folder with PHP - Learn how to count the number of files currently in a directory using PHP

Count number of files in a folder with PHP

On my main page i added the number of image files i currently host for other people. I did this by using some very simple code. You might need this to count logs or images like me. Or maybe you just want to know how many video files you have in your folder on your hosting account.

Either way, this code is very basic and simple and can be adjusted a lot further to make it even more advanced. Feel free to do just that and to let us know by leaving a comment down here.

You can add the following code anywhere you want on your page. Keep in mind that nothing has to be kept this way. Change some things, add some things. Don't just copy and paste and not learn :)

Now for the code:

<?

$d = opendir("foldername");
$count = 0;

while(($f = readdir($d)) !== false)
if(ereg('.jpg$', $f))
++$count;
closedir($d);

print "$count";

?>

As you can see there's not much to say about it. It's simple and to the point. Feel free to give suggestions by leaving a comment.

 

template error, examine fetch() result