Php get folder size

How to get Folder or Directory size in PHP Laravel?

How to get Folder or Directory size in PHP Laravel?

In this tutorial, i am going to tell you how to get directory size in PHP Laravel.

As all knows, PHP has inbuilt function to get file size using filesize() method but ever you think how to get directory size in PHP or how to read directory files and sub directory within directory?

A folder may have many files and have subfolder that contains many files too.

So here you will know how to iterate over folders and their files.

PHP function will return size in bytes so first we write a function to convert bytes to Kilobyte, Megabyte, Gigabyte and Terabyte etc.

PHP Function to format size

function formatSize($bytes)< $kb = 1024; $mb = $kb * 1024; $gb = $mb * 1024; $tb = $gb * 1024; if (($bytes >= 0) && ($bytes < $kb)) < return $bytes . ' B'; >elseif (($bytes >= $kb) && ($bytes < $mb)) < return ceil($bytes / $kb) . ' KB'; >elseif (($bytes >= $mb) && ($bytes < $gb)) < return ceil($bytes / $mb) . ' MB'; >elseif (($bytes >= $gb) && ($bytes < $tb)) < return ceil($bytes / $gb) . ' GB'; >elseif ($bytes >= $tb) < return ceil($bytes / $tb) . ' TB'; >else < return $bytes . ' B'; >>

Now we will write a function to scan directory and return size of directory.

Читайте также:  Как создать моды на майнкрафт java

PHP Function to return directory size

function folderSize($dir)< $total_size = 0; $count = 0; $dir_array = scandir($dir); foreach($dir_array as $key=>$filename)< if($filename!=".." && $filename!=".")< if(is_dir($dir."/".$filename))< $new_foldersize = foldersize($dir."/".$filename); $total_size = $total_size+ $new_foldersize; >else if(is_file($dir."/".$filename)) < $total_size = $total_size + filesize($dir."/".$filename); $count++; >> > return $total_size; >
$folder_path = "folder path"; echo formatSize(folderSize($folder_path));

Now you can easily scan your directory using PHP inbuilt function and get size of directory.

Источник

Get Folder or Directory size in PHP?

In PHP we have an inbuilt function called filesize() to get the size of any file, but what about a directory/ folder. A folder can content many files and sub folders in it. So lets write down a simple PHP function to calculate the folder size.

PHP Function

function folderSize($dir)< $count_size = 0; $count = 0; $dir_array = scandir($dir); foreach($dir_array as $key=>$filename)< if($filename!=".." && $filename!=".")< if(is_dir($dir."/".$filename))< $new_foldersize = foldersize($dir."/".$filename); $count_size = $count_size+ $new_foldersize; >else if(is_file($dir."/".$filename)) < $count_size = $count_size + filesize($dir."/".$filename); $count++; >> > return $count_size; >

Call in Action

Output

The above function will return the folder size in bytes. So we need to write another function which will convert the bytes to Kilobyte, Megabyte, Gigabyte, Terabyte etc.

PHP Function to format size

function sizeFormat($bytes)< $kb = 1024; $mb = $kb * 1024; $gb = $mb * 1024; $tb = $gb * 1024; if (($bytes >= 0) && ($bytes < $kb)) < return $bytes . ' B'; >elseif (($bytes >= $kb) && ($bytes < $mb)) < return ceil($bytes / $kb) . ' KB'; >elseif (($bytes >= $mb) && ($bytes < $gb)) < return ceil($bytes / $mb) . ' MB'; >elseif (($bytes >= $gb) && ($bytes < $tb)) < return ceil($bytes / $gb) . ' GB'; >elseif ($bytes >= $tb) < return ceil($bytes / $tb) . ' TB'; >else < return $bytes . ' B'; >>

Источник

Get Folder Size in PHP

Most of the web hosting company does not provide unlimited FTP spaces. In order to monitor the usage, you can login to web-based control panel and view the usage statistics.

Another handy method would be to use PHP to compute the folder sizes. The following is a function to recursively compute the size in bytes for a web-folder. Of course, base on this function, you can easily create events of notification (such as sending emails when usage is high).

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
function dirsize($dir)  @$dh = opendir($dir); $size = 0; while ($file = @readdir($dh))  if ($file != "." and $file != "..")  $path = $dir."/".$file; if (is_dir($path))  $size += dirsize($path); // recursive in sub-folders > elseif (is_file($path))  $size += filesize($path); // add file > > > @closedir($dh); return $size; >
function dirsize($dir) < @$dh = opendir($dir); $size = 0; while ($file = @readdir($dh)) < if ($file != "." and $file != "..") < $path = $dir."/".$file; if (is_dir($path)) < $size += dirsize($path); // recursive in sub-folders >elseif (is_file($path)) < $size += filesize($path); // add file >> > @closedir($dh); return $size; >

Another simpler solution from realmag777 that utilities the latest PHP language feature:

1 2 3 4 5 6 7 8 9 10 11 12 13
/* * * * Get the directory size * @param directory $directory * @return integer */ function get_dir_size($directory)  $size = 0; foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file)  $size += $file->getSize(); > return $size; >
/* * * * Get the directory size * @param directory $directory * @return integer */ function get_dir_size($directory) < $size = 0; foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) < $size += $file->getSize(); > return $size; >

Two PHP Functions to Check HTTP Response Code (Status) of a URL using Curl Check the HTTP Reponse Status Code of a Request in PHP We can use the…

Left and Right Function in PHP We all know in Visual Basic or VBScript (including VBA i.e. Visual Basic for Application,…

Index Sorting in PHP Index Sort is not a general sorting algorithm because it has a limitation that is…

PHP Function to Post to Twitter In order to post twits to Twitter using Twitter APIs, you would first need to…

Test Javascript On/Off in PHP via POST Javascript is almost enabled in modern browsers. However, at some legacy devices or some particular…

Database Optimisation Script in PHP Well, sometimes, you probably need to optimise your MySQL tables. In order to avoid the…

STDIN and STDOUT in PHP Well, I think PHP is great, but apparently somebody thinks that it is replacble in…

How to Implement file_put_contents and file_get_contents in PHP? We can implement (re-invent the wheel) file_put_contents and file_get_contents in PHP to write string to…

PHP Speed Improvement Tips PHP is a popular web programming language that has been used in millions of website.…

One Response

Here is more simply:
/* * *
* Get the directory size
* @param directory $directory
* @return integer
*/ function get_dir_size($directory) $size = 0;
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) $size+=$file->getSize();
>
return $size;
>

Источник

Calculate folder and subfolders size with PHP

How to read the size of a directory using PHP? Here is a simple function which could help read the size of the directory, number of directories and the number of files in the given directory.

Those days i was wondering whether i could read the size of a directory using PHP. So i thought of making something which could help me read the size of the directory, number of directories and the number of files in the given directory.

So here are my 2 little functions doing that bit:

The function “filesize_recursive” will ignore link/shortcuts to files/directory.
The function “display_size” will suffix the size with bytes, KB, MB or GB accordingly.

 < ?php $path = "/path-to-folder"; // function display_size($size) < // Function 2 $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); if ($retstring === null) < $retstring = '%01.2f %s'; >$lastsizestring = end($sizes); foreach ($sizes as $sizestring) < if ($size < 1024) < break; >if ($sizestring != $lastsizestring) < $size /= 1024; >> if ($sizestring == $sizes[0]) < $retstring = '%01d %s'; >// Bytes aren't normally fractional return sprintf($retstring, $size, $sizestring); > echo "Folder size: ".display_size(filesize_recursive($path)).""; ?>

Depending on the size of a folder, is it possible to display the size value in MegaBytes, GigaBytes, or TeraBytes, including the appropriate abbreviations MB, GB, TB, etc ..

Источник

Оцените статью