ItSolutionStuff.com

How to Count Number of Files in a Directory in PHP?

By Hardik Savani • May 14, 2024
PHP

In PHP, you are work on php and you want to need display count number of files in specific folder then you do, glob() through you count number of file are there in specified folder. now in following example you can see "/var/www/test" in this folder, count how many files are there in that folder. glob function take a two argument first one for directory or folder path and second argument for filter. Filter means if you pass '*' then it will count all type of file, i mean all extension, but if you need to count just text file then you can put like '*.txt'.

That't it, if in your directory, function does not find any file then it will always return "false". you can check following example and try that...

Example 1:

index.php

<?php

$folderPath = '/folderName';

$file = glob($folderPath . '*');

$countFile = 0;

if ($file != false){

$countFile = count($file);

}

print_r($countFile);

?>

Output:

10

I hope it can help you...

Tags: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Check Current PHP Version in Ubuntu?

Read Now →

PHP nl2br() Function Example Tutorial

Read Now →

PHP Google Recaptcha V2 Example Tutorial

Read Now →

PHP implode() Function Example Tutorial

Read Now →

How to Convert Array to String in PHP?

Read Now →

PHP - How to Remove Blank Pages from PDF File?

Read Now →

How to Install PHP in Ubuntu Server?

Read Now →

How to Get Month Name from Date in PHP?

Read Now →

PHP Subtract Seconds from Time Example

Read Now →

How to Subtract Minutes from DateTime in PHP?

Read Now →

PHP Subtract Year from Date Example

Read Now →

How to Find Day Name from Specific Date in PHP?

Read Now →

How to Remove Null Values from Array in PHP?

Read Now →