NameSilo

How to show how many files are in a directory thread

Spaceship Spaceship
Watch
how to show how many files are in a directory

Does anyone have the code to display how many files are in a folder on your website?

Example if i stored my images in a folder called images:

We currently host: 80 photos

Any idea how to do this?

Thanks

Joseph
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains โ€” AI StorefrontUnstoppable Domains โ€” AI Storefront
Here's a (modified) example from php.net

PHP:
<?php
$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           $num_of_files++;
       }
       closedir($dh);
   }
}
echo "We host: $num_of_files";
?>
 
0
•••
Great!!
 
0
•••
Thanks :)
 
0
•••
And here's a modified version of Tree's modified PHP.net version...;)
PHP:
<?php

$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if(is_dir($dir))
{
  if($dh = opendir($dir))
  {
    while(($file = readdir($dh)) !== false)
    {
      if($file != "." && $file != "..")
      {
        $num_of_files++;
      }
    }
    closedir($dh);
  }
}
echo "We host: $num_of_files";

?>
 
0
•••
Yeah, I guess SecondVersion's is better. Mine would be off by 2 if you're using Apache.
 
0
•••
thanks to all, using SV's =)
 
0
•••
You could put it in a recursive function, and count files in subfolders as well.
 
0
•••
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back