Current Location: Home> Latest Articles> PHP Script to Calculate Folder Information: Folder Count, File Count, and Folder Size

PHP Script to Calculate Folder Information: Folder Count, File Count, and Folder Size

gitbox 2025-06-18

1. Calculating Folder Information in PHP

In PHP, you can use filesystem functions to calculate information about a specified folder, including the number of subfolders, the number of files, and the total folder size. By using a recursive approach, we can traverse all files and subfolders inside the target folder to gather relevant information.

1.1 Recursively Traverse the Folder

First, we need to write a recursive function that traverses all files and subfolders inside a folder. This function will take one parameter, which is the path of the folder to be scanned.

Here’s the recursive function to traverse the folder:

function getFolderInfo($folderPath) {
    $folderCount = 0;  // Folder count
    $fileCount = 0;    // File count
    $folderSize = 0;   // Folder size

    $files = scandir($folderPath);  // Get all files and subfolders in the directory

    foreach ($files as $file) {
        if ($file == '.' || $file == '..') {  // Ignore the current and parent directories
            continue;
        }

        $filePath = $folderPath . '/' . $file;  // Get full path of the file or subfolder

        if (is_dir($filePath)) {  // If it's a subfolder
            $folderCount++;  // Increment folder count
            $subFolderInfo = getFolderInfo($filePath);  // Recursively get subfolder info
            $folderCount += $subFolderInfo['folderCount'];  // Add subfolder's folder count
            $fileCount += $subFolderInfo['fileCount'];  // Add subfolder's file count
            $folderSize += $subFolderInfo['folderSize'];  // Add subfolder's folder size
        } else {  // If it's a file
            $fileCount++;  // Increment file count
            $folderSize += filesize($filePath);  // Add file size to folder size
        }
    }

    return [
        'folderCount' => $folderCount,
        'fileCount' => $fileCount,
        'folderSize' => $folderSize
    ];
}

In the above code, we first initialize folder count, file count, and folder size to zero. Then, using the scandir function, we retrieve all files and subfolders in the directory. We then loop through each file and subfolder, checking their type and processing them accordingly.

If the item is a folder, we recursively call the getFolderInfo function to get the information of subfolders, and then add the subfolder counts and sizes to the current folder's values.

If it's a file, we increment the file count and add its size to the current folder's size.

1.2 Statistically Gathering Folder Information

To gather the information for a specific folder, we simply call the getFolderInfo function and pass the path of the folder we want to analyze.

$folderPath = '/path/to/folder';  // Path to the folder
$folderInfo = getFolderInfo($folderPath);
$folderCount = $folderInfo['folderCount'];  // Folder count
$fileCount = $folderInfo['fileCount'];  // File count
$folderSize = $folderInfo['folderSize'];  // Folder size

In the code above, we specify the folder path to be analyzed, call the getFolderInfo function, and then store the results for folder count, file count, and folder size in respective variables.

2. Example

Below is an example demonstrating how to use the above code to calculate the folder information.

$folderPath = '/path/to/folder';  // Specify the folder path
$folderInfo = getFolderInfo($folderPath);
$folderCount = $folderInfo['folderCount'];  // Folder count
$fileCount = $folderInfo['fileCount'];  // File count
$folderSize = $folderInfo['folderSize'];  // Folder size

echo "Folder count: $folderCount\n";
echo "File count: $fileCount\n";
echo "Folder size: $folderSize bytes\n";

When running the above code, assuming you want to check the folder /path/to/folder, it will output the following results:

Folder count: 3
File count: 10
Folder size: 123456789 bytes

This result indicates that the folder /path/to/folder contains 3 subfolders, 10 files, and has a total size of 123,456,789 bytes.

3. Summary

By utilizing recursion and filesystem functions, we can easily calculate the number of subfolders, files, and the total size of a specified folder. The code provided helps us understand the structure of the folder and its space usage, making it easier to manage and optimize file storage.