Specify the number of files in php directory

I am working on a slightly new project. I wanted to know how to make it count how many files are in a particular directory.

<div id="header"> <?php $dir = opendir('uploads/'); # This is the directory it will count from $i = 0; # Integer starts at 0 before counting # While false is not equal to the filedirectory while (false !== ($file = readdir($dir))) { if (!in_array($file, array('.', '..') and !is_dir($file)) $i++; } echo "There were $i files"; # Prints out how many were in the directory ?> </div> 

This is what I have so far (from search). However, he does not look right? I added a few notes, so feel free to delete them, they just so I can understand it as best as possible.

If you need more information or I feel as if I have not described it enough, feel free to state it.

+69
file directory php count
Oct 09
source share
11 answers

You can simply do the following:

 $fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS); printf("There were %d Files", iterator_count($fi)); 
+195
09 Oct '12 at 14:01
source share

You can get a file like this:

 $directory = "/path/to/dir/"; $filecount = 0; $files = glob($directory . "*"); if ($files){ $filecount = count($files); } echo "There were $filecount files"; 

where "*" you can change this to a specific file type if you want, like "*.jpg" , or you can make several file types, for example:

 glob($directory . "*.{jpg,png,gif}",GLOB_BRACE) 

the GLOB_BRACE flag extends {a, b, c} to match 'a', 'b' or 'c'

+42
09 Oct
source share

You must have:

 <div id="header"> <?php // integer starts at 0 before counting $i = 0; $dir = 'uploads/'; if ($handle = opendir($dir)) { while (($file = readdir($handle)) !== false){ if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) $i++; } } // prints out how many were in the directory echo "There were $i files"; ?> </div> 
+41
09 Oct
source share

Try it.

 // Directory $directory = "/dir"; // Returns array of files $files = scandir($directory); // Count number of files and store them to variable.. $num_files = count($files)-2; 

Not to mention '.' and "..".

+26
Oct 09 '12 at 13:41
source share

Working demo

 <?php $directory = "../images/team/harry/"; // dir location if (glob($directory . "*.*") != false) { $filecount = count(glob($directory . "*.*")); echo $filecount; } else { echo 0; } ?> 
+12
Oct 09
source share

Since I needed this too, I was curious about which alternative was the fastest.

I found that if all you need is the number of files - Baba's solution is much faster than others. I was very surprised.

Try it yourself:

 <?php define('MYDIR', '...'); foreach (array(1, 2, 3) as $i) { $t = microtime(true); $count = run($i); echo "$i: $count (".(microtime(true) - $t)." s)\n"; } function run ($n) { $func = "countFiles$n"; $x = 0; for ($f = 0; $f < 5000; $f++) $x = $func(); return $x; } function countFiles1 () { $dir = opendir(MYDIR); $c = 0; while (($file = readdir($dir)) !== false) if (!in_array($file, array('.', '..'))) $c++; closedir($dir); return $c; } function countFiles2 () { chdir(MYDIR); return count(glob("*")); } function countFiles3 () // Fastest method { $f = new FilesystemIterator(MYDIR, FilesystemIterator::SKIP_DOTS); return iterator_count($f); } ?> 

Testing: (obviously, glob() does not take dot files into account)

 1: 99 (0.4815571308136 s) 2: 98 (0.96104407310486 s) 3: 99 (0.26513481140137 s) 
+12
Oct 22 '13 at 9:29
source share

I use this:

 count(glob("yourdir/*",GLOB_BRACE)) 
+4
Mar 07 '14 at 5:45
source share
 <?php echo(count(array_slice(scandir($directory),2))); ?> 

array_slice works similarly to the substr function, only it works with arrays.

For example, this will result in shredding the first two keys of the array from the array:

 $key_zero_one = array_slice($someArray, 0, 2); 

And if you omit the first parameter, as in the first example, the array will not contain the first two key / value pairs * ('.' And '..').

+1
Apr 09 '16 at 21:52
source share
 $it = new filesystemiterator(dirname("Enter directory here")); printf("There were %d Files", iterator_count($it)); echo("<br/>"); foreach ($it as $fileinfo) { echo $fileinfo->getFilename() . "<br/>\n"; } 

This should work enter the directory in dirname. and let the magic happen.

0
Nov 16 '15 at 10:32
source share

Maybe useful to someone. On Windows, you can let Windows complete the task by calling the dir command. I use an absolute path like E:/mydir/mysubdir .

 <?php $mydir='E:/mydir/mysubdir'; $dir=str_replace('/','\\',$mydir); $total = exec('dir '.$dir.' /b/ad | find /v /c "::"'); 
0
Aug 17 '17 at 8:59
source share
  simple code add for file .php then your folder which number of file to count its $directory = "images/icons"; $files = scandir($directory); for($i = 0 ; $i < count($files) ; $i++){ if($files[$i] !='.' && $files[$i] !='..') { echo $files[$i]; echo "<br>"; $file_new[] = $files[$i]; } } echo $num_files = count($file_new); 

just add it .....

-one
Jan 02 '15 at 13:37
source share



All Articles