find file size directory size using php

Last updated on January 20th, 2023 at 09:45 am

In this tutorial we are going to find the size of each files within a folder and then display the total size of folder or directory.

We are using 4 inbuilt functions for this

  • opendir() – Open the directory
  • readdir() – Read directory
  • filesize() – Find file size

Very first step is to provide the path of the directory you would like to find the size and then use opendir(). Replace the $dir with your own path. Give absolute or relative path.

In the example I am showing, my directory size check script itself is residing in a folder named /var/www/web/size and there is another folder inside that path itself named check (/var/www/web/size/check) . Name of my script is folder_size_check.php

If you are using windows provide the path accordingly.

$dir="check/";
$handle = @opendir($dir);

Now that we have opened the directory the next step is to read through the directory and find all the files inside it. We are using preg_match with condition to check .(dot) and ..(dot dot) and ignore them while listing the files.

while ($file = @readdir ($handle))
{
        if (!preg_match("/^\.{1,2}$/",$file)) {
        echo $file." ";
}
}

In unix like systems all directories contain two entries, . and .., which stand for the directory itself and its parent respectively.

Now that we have the file listed lets use filesize() to get the size of each files

if (!preg_match("/^\.{1,2}$/",$file)) {
        echo $file." ";
        $size=filesize($dir.$file);
	echo $size." Bytes which is ";
        $gbsize= ($size/1000000000);
	$size_gb = number_format((float)$gbsize, 2, '.', '');
	echo $size_gb." GB";
	echo "<hr>";
    $totalsize=$totalsize+$size;
        }

As you can see above I got the size of files in bytes initially and then converted in to GB (Dividing it by 1000000000). In order to just show two decimals I used number_format(). The $totalsize variable has the size of all the files within the directory

Complete script

<?php
$totalsize=0;
//i have given  c:/Program Files/xampp/htdocs/Ajax Test/
$dir="check/";
$handle = @opendir($dir);
while ($file = @readdir ($handle))
{
        if (!preg_match("/^\.{1,2}$/",$file)) {
        echo $file." ";
        $size=filesize($dir.$file);
	echo $size." Bytes which is ";
        $gbsize= ($size/1000000000);
	$size_gb = number_format((float)$gbsize, 2, '.', '');
	echo $size_gb." GB";
	echo "<hr>";
	$totalsize=$totalsize+$size;
        }
}
echo $totalsize;
?>

Now that we have the complete script lets walk through an example.

As discussed above this is how my script (folder_size_check.php) is deployed

root@/var/www/web/size# ls -rlt
total 8
-rw-r--r-- 1 root root  638 Jan 20 17:09 folder_size_check.php
drwxr-xr-x 2 root root 4096 Jan 20 17:13 check
root@/var/www/web/size# cd check/
root@/var/www/web/size/check# ls -rlt
total 2277388
-rw-r--r-- 1 root root 1073741824 Jan 20 16:10 test_file
-rw-r--r-- 1 root root  524288000 Jan 20 17:03 test_file2
-rw-r--r-- 1 root root  734003200 Jan 20 17:13 test_file3

There is a check directory and within that directory I have 3 files

When I run the PHP script, the output I will be getting is

As you can see above the script show size of each files along with the total size in Bytes as well as Gigabytes.

Demo

One thought on “How to find size of a directory using php”
  1. As long as you change all the ” marks to the correct ascii character, this works a treat on both harddisk directories and on the web.

    To view a web directory, change
    $totalsize = show_dir(“c:/Program Files/xampp/htdocs/Ajax Test/”);
    to:
    $totalsize = show_dir(“http://website.com/directory/”);

    To view the local directory where the script is being run, change it to:
    $totalsize = show_dir(“./”);

Leave a Reply

Your email address will not be published. Required fields are marked *