PHP: How to count files in folder?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this blog i will show you how to count files in folder by using php. When we are work on php and we need to display count number of files in specific folder then we can do, glob() through we can count number of file are there in specified folder. So now in following example we can see “/var/www/test” in this folder, here we count how many files are there in that folder. Here glob function take a two argument first one for directory or folder path and then second argument for filter. Filter means if you pass ‘*’ then it will also count all type of file, i mean all extension, but if we need to count just text file then you can put like ‘*.txt’.

So That’t it, if in our directory, function can not find any file then it will always us return “false”. We can check the following example and try that…

Example:

$folderPath = '/var/www/test';
$file = glob($folderPath . '*');
$countFile = 0;
if ($file != false)
{
    $countFile = count($file);
}
print_r($countFile);

Read Also : How to Open Link in New Window by using Javascript ?

Thanks for read. I hope it help you. For more you can follow us onĀ facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →