471,888 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,888 software developers and data experts.

how to check folder size

exoskeleton
104 100+
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
10 25448
ronverdonk
4,258 Expert 4TB
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
104 100+
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
104 100+
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
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
6,050 Expert 4TB
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
4,258 Expert 4TB
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
7effrey
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
4,258 Expert 4TB
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
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
JSusi
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.

Similar topics

1 post views Thread by Sunny Shah | last post: by
reply views Thread by Kevin F | last post: by
1 post views Thread by Kevin F | last post: by
3 posts views Thread by TyBreaker | last post: by
4 posts views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.