How to count number of files in a directory using PHP

No comments
If you are creating a gallery page or creating a job portal where a candidate can upload their CVs and other files in different formats and suddenly your boss asked you to tell him how many profiles have been created with the CVs, in this situation you have two options either you count the files one by one or you just simply run a PHP code that will return you number of files uploaded. Even, you can create a module in your application that will tell you how many profiles are created till date who have uploaded their CVs. So, here in this post i am going to share with you a small but useful PHP code that will help you in counting number of files uploaded in a directory.
$countfiles = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
printf("There were %d CVs", iterator_count($countfiles));

In above code, i have used PHP inbuilt class FilesystemIterator.

Please note that, this code will only work if you server have installed PHP 5.3 or higher. 

No comments :

Post a Comment