473,389 Members | 1,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,389 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 25654
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

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

Similar topics

1
by: Sunny Shah | last post by:
hello php programmers & friends, i want to limit the size of my folder. is there any code in php or other language to check the size n limit it or display warning msg. please guide me. ...
4
by: Hughs Man | last post by:
I am using exchange 2003 now Recently I was notified the harddisk is out of space. Thus I remove part of the email of each folder and the public folder. But the folder size doesn't trim down. ...
0
by: Kevin F | last post by:
I'm trying to use the following code to get my remote server's folder size information. Unfortunately, i'm getting the error: Traceback (most recent call last): File...
1
by: Kevin F | last post by:
I'm trying to use the following code to get my remote server's folder size information. Unfortunately, i'm getting the error: Traceback (most recent call last): File...
3
by: TyBreaker | last post by:
I have a vbscript which returns the folder size of any folder instantly by referring to Folder.Size. I don't need any iteration or recursion through the subfolders, adding together individual file...
7
by: ethanhines | last post by:
I am using MS Access 2007, I want to change the size of the Check Box that is displayed when a Value List is displayed for multiple selection. I changed the font size but I need to change the Check...
4
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post by:
Hi all how can I get folder size with high performance ?? Do I need use recursive method for all files-subfolders within root folder ?? Anyway for do it ?? Perhaps using API Windows ?? ...
3
Frinavale
by: Frinavale | last post by:
Hi there! I'm hoping that someone knows how to check the size of a file before it is uploaded to the server using JavaScript. I have seen suggested solutions using an ActiveX control to check...
7
by: jeddiki | last post by:
I want to check the size of an image file before I write it. If it is a certain size, then I won't write it. Can I check the future size of the file by using strlen() on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.