How to get Folder or Directory size in PHP Laravel – onlinecode

How to get Folder or Directory size in PHP Laravel – onlinecode

In this post we will give you information about How to get Folder or Directory size in PHP Laravel – onlinecode. Hear we will give you detail about How to get Folder or Directory size in PHP Laravel – onlinecodeAnd how to use it also give you demo for it if it is necessary.

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

  1. functionformatSize($bytes){
  2. $kb=1024;
  3. $mb=$kb*1024;
  4. $gb=$mb*1024;
  5. $tb=$gb*1024;
  6. if(($bytes>=)&&($bytes<$kb)){
  7. return$bytes.' B';
  8. }elseif(($bytes>=$kb)&&($bytes<$mb)){
  9. returnceil($bytes/$kb).' KB';
  10. }elseif(($bytes>=$mb)&&($bytes<$gb)){
  11. returnceil($bytes/$mb).' MB';
  12. }elseif(($bytes>=$gb)&&($bytes<$tb)){
  13. returnceil($bytes/$gb).' GB';
  14. }elseif($bytes>=$tb){
  15. returnceil($bytes/$tb).' TB';
  16. }else{
  17. return$bytes.' B';
  18. }
  19. }
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.


PHP Function to return directory size

  1. functionfolderSize($dir){
  2. $total_size=;
  3. $count=;
  4. $dir_array=scandir($dir);
  5. foreach($dir_arrayas$key=>$filename){
  6. if($filename!=".."&&$filename!="."){
  7. if(is_dir($dir."/".$filename)){
  8. $new_foldersize=foldersize($dir."/".$filename);
  9. $total_size=$total_size+$new_foldersize;
  10. }elseif(is_file($dir."/".$filename)){
  11. $total_size=$total_size+filesize($dir."/".$filename);
  12. $count++;
  13. }
  14. }
  15. }
  16. return$total_size;
  17. }
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;
}


Define Folder and pass it to function to get size

  1. $folder_path="folder path";
  2. echoformatSize(folderSize($folder_path));
  $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.

Hope this code and post will helped you for implement How to get Folder or Directory size in PHP Laravel – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

29  +    =  39

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US