How to Count Number of Files in Directory using PHP?

By Hardik Savani May 14, 2024 Category : PHP

Hey Dev,

In this post, we will learn count number of files in directory php. you can see count number of files in folder php. you'll learn count number of images in folder php. It's a simple example of count number of images in a directory php. Let's see below example php count number of files in directory.

If you are working on code php and you require to count a number of files, images, zip files, rar files, etc in the given folder path, then you can count using glob() and count() of PHP. So, let's see the below example and learn how it did.

Example:

index.php

<?php

$folderPath = "upload/";

$countFile = 0;

$totalFiles = glob($folderPath . "*");

if ($totalFiles){

$countFile = count($totalFiles);

}

print_r($countFile);

?>

Output:

10

I hope it can help you...

Tags :
Shares