By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,637 Members | 3,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,637 IT Pros & Developers. It's quick & easy.

how to check folder size

exoskeleton
100+
P: 104
hi dear experts,

hope you can help about checking the folder size,for example how many kilobytes is it already in PHP script...is it possible?

please help..thank you
Jan 26 '07 #1
Share this Question
Share on Google+
10 Replies


ronverdonk
Expert 2.5K+
P: 4,258
This little snippet, by Jonas Sweden in the PHP documentation, adds the sizes of all files in the folder and show the total bytes.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $path = "gal";
  3. echo "Folder $path = ".filesize_r($path)." bytes";
  4.  
  5. function filesize_r($path){
  6.   if(!file_exists($path)) return 0;
  7.   if(is_file($path)) return filesize($path);
  8.   $ret = 0;
  9.   foreach(glob($path."/*") as $fn)
  10.     $ret += filesize_r($fn);
  11.   return $ret;
  12. }
  13. ?>
Ronald :cool:
Jan 26 '07 #2

exoskeleton
100+
P: 104
thank you very much sir ronverdonk...i will try the code right away...

OT: i read some threads, hehe...you like motoma? peace!!! :)
Jan 27 '07 #3

exoskeleton
100+
P: 104
This little snippet, by Jonas Sweden in the PHP documentation, adds the sizes of all files in the folder and show the total bytes.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $path = "gal";
  3. echo "Folder $path = ".filesize_r($path)." bytes";
  4.  
  5. function filesize_r($path){
  6.   if(!file_exists($path)) return 0;
  7.   if(is_file($path)) return filesize($path);
  8.   $ret = 0;
  9.   foreach(glob($path."/*") as $fn)
  10.     $ret += filesize_r($fn);
  11.   return $ret;
  12. }
  13. ?>
Ronald :cool:
thank you thank you thank you sir ronverdonk...IT WORKS!!! :) good luck about..... :)
Jan 27 '07 #4

P: 1
Code works fantastically!

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
Feb 24 '08 #5

Markus
Expert 5K+
P: 6,050
Code works fantastically!

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
Divide the bytes by 1024 to get kilobytes
Expand|Select|Wrap|Line Numbers
  1. $_bytes = $_FILES['file']['size'];
  2. echo ($_bytes / 1024) . "Kb";
  3.  
Feb 24 '08 #6

ronverdonk
Expert 2.5K+
P: 4,258
Code works fantastically!

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
Thanks you KINGMAX. That's what we are here for.
And to wrap it up: here a little function that shows you the size in Gb, Mb, Kb or plain bytes.You can work out the Terabytes for yourself.
Expand|Select|Wrap|Line Numbers
  1. function showSize($size_in_bytes) {
  2.     $value = 0;        
  3.     if ($size_in_bytes >= 1073741824) {
  4.         $value = round($size_in_bytes/1073741824*10)/10;
  5.         return  ($round) ? round($value) . 'Gb' : "{$value} gB";
  6.     } else if ($size_in_bytes >= 1048576) {
  7.         $value = round($size_in_bytes/1048576*10)/10;
  8.         return  ($round) ? round($value) . 'Mb' : "{$value} mB";
  9.     } else if ($size_in_bytes >= 1024) {
  10.         $value = round($size_in_bytes/1024*10)/10;
  11.         return  ($round) ? round($value) . 'Kb' : "{$value} kB";
  12.     } else {
  13.         return "$size_in_bytes bytes";
  14.     }
  15. }
Ronald
Feb 24 '08 #7

P: 12
Thanks you KINGMAX. That's what we are here for.
And to wrap it up: here a little function that shows you the size in Gb, Mb, Kb or plain bytes.You can work out the Terabytes for yourself.
Expand|Select|Wrap|Line Numbers
  1. function showSize($size_in_bytes) {
  2.     $value = 0;        
  3.     if ($size_in_bytes >= 1073741824) {
  4.         $value = round($size_in_bytes/1073741824*10)/10;
  5.         return  ($round) ? round($value) . 'Gb' : "{$value} gB";
  6.     } else if ($size_in_bytes >= 1048576) {
  7.         $value = round($size_in_bytes/1048576*10)/10;
  8.         return  ($round) ? round($value) . 'Mb' : "{$value} mB";
  9.     } else if ($size_in_bytes >= 1024) {
  10.         $value = round($size_in_bytes/1024*10)/10;
  11.         return  ($round) ? round($value) . 'Kb' : "{$value} kB";
  12.     } else {
  13.         return "$size_in_bytes bytes";
  14.     }
  15. }
Ronald
how will the whole script looks like?
Mar 2 '08 #8

ronverdonk
Expert 2.5K+
P: 4,258
What script? This is just a sample you can use in your own application to show the size of folders. You'll have to write the script, in which you want to use it, yourself.

Ronald
Mar 2 '08 #9

P: 1
how will the whole script looks like?
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $path = "myTargetedFolderNameHere"; // <-- Edit: Add your folder name here!
  3.  
  4. function filesize_r($path){ // Function 1
  5. if(!file_exists($path)) return 0;
  6. if(is_file($path)) return filesize($path);
  7. $ret = 0;
  8. foreach(glob($path."/*") as $fn)
  9. $ret += filesize_r($fn);
  10. return $ret;
  11. }
  12.  
  13. function showSize($size_in_bytes) { // Function 2
  14. $value = 0;  
  15. if ($size_in_bytes >= 1073741824) {
  16. $value = round($size_in_bytes/1073741824*10)/10;
  17. return  ($round) ? round($value) . 'Gb' : "{$value} Gb";
  18. } else if ($size_in_bytes >= 1048576) {
  19. $value = round($size_in_bytes/1048576*10)/10;
  20. return  ($round) ? round($value) . 'Mb' : "{$value} Mb";
  21. } else if ($size_in_bytes >= 1024) {
  22. $value = round($size_in_bytes/1024*10)/10;
  23. return  ($round) ? round($value) . 'Kb' : "{$value} Kb";
  24. } else {
  25. return "{$size_in_bytes} Bytes";
  26. }
  27. }
  28.  
  29. echo "Folder {$path} size: <b>".showSize(filesize_r($path))."</b>";
  30. ?>
  31.  
Aug 13 '08 #10

P: 1
I found this in the web one day and it works fine for me.
Expand|Select|Wrap|Line Numbers
  1. function getSymbolByQuantity($bytes)
  2. {
  3.     $symbols = array('B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb');
  4.     $exp = floor(log($bytes)/log(1024));
  5.  
  6.     return sprintf("%.2f " . $symbols[$exp], ($bytes/pow(1024, floor($exp))));
  7. }
Feb 5 '09 #11

Post your reply

Sign in to post your reply or Sign up for a free account.