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

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

PHP Function to return directory size
  1. function folderSize($dir){
  2. $total_size = 0;
  3. $count = 0;
  4. $dir_array = scandir($dir);
  5. foreach($dir_array as $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. }else if(is_file($dir."/".$filename)){
  11. $total_size = $total_size + filesize($dir."/".$filename);
  12. $count++;
  13. }
  14. }
  15. }
  16. return $total_size;
  17. }
Define Folder and pass it to function to get size
  1. $folder_path = "folder path";
  2. echo formatSize(folderSize($folder_path));

Phone: (+91) 8800417876
Noida, 201301
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort
ataşehir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort
bedava bahis