Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
code to show size of specified folder?
Old 01-23-2007, 04:31 PM code to show size of specified folder?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Hi,
im trying to make a simple control panel.
and i want to show how bit a folder is.
so i need a code which reads a specified folder size and display's it anyone?...

Thanks
dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 01-23-2007, 05:10 PM Re: code to show size of specified folder?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Well....

To do that, you need a recursive function, that will go down your folders, and sum up all files size.

first, create a function like that one:

!!!! Read this !!!
This is conceptual. I've wrote that from scratch and memory, there might be bug or problems with it. Take it for what it is, a big fat panel to get you on the good way.

PHP Code:
<?
/**
 * Return the byte size of every files and directory from the starting dir given.
 * Returns 0 on empty directory. Returns FALSE on an unexisting on unreadable directory
 * @param     String     $path     The initial directory path, relative to the file system. Not relative to the web server
* @return      Mixed      Return FALSE when not a directory or directory not readable, and an Int whith the byte size otherwise
 */
function getDirSize($path){
    
$ret=false;
    
    
//We first check the directory, to see if it exists and that we can read it
    
if( (!is_dir($path)) || (!is_readable($path)) ){
        return 
$ret;
    }
    else{
        
//There is a directory, if it's empty, his size will be 0
        
$ret=0;
    }
    
$dirH=opendir($path);
    while (
false !== ($file readdir($dirH))) {
        if (
$file != "." && $file != "..") {
            
//We have a file or a directory, and it's not the parent neither itself.
            //If it's a directory, we call this function again to get dow it.
            
if(is_dir($file)){
            
$ret+=getDirSize($file);
            }
            else{
                
//This is a file, we add his size to the rest
                
$ret+=filesize($file);
            }
        }
    }
    
closedir($dirH);
    
    return 
$ret;
}
?>
This will/should return you the total size of your directoy and every directory and files below that point.
Just keep in mind that if you give the wrong directory (like, /) this can fail because of the timeout prevention of PHP (30 second run time per script, per default).

Ultimately, this is a function I carry around for some times now, to format byte size in human readable sizes:
PHP Code:
/**
 * Format a file size in ah human readable way
 * 
 * @param     Int          $size The file size
 * @return    String      An human readable converted size
 */
function filesize_hum($size){
  
$i=0;
  
$iec = array("B""KB""MB""GB""TB""PB""EB""ZB""YB");
  while ((
$size/1024)>1) {
   
$size=$size/1024;
   
$i++;
  }
  
$val=round(substr($size,0,strpos($size,'.')+4),2);
  
$size=$iec[$i];
  return 
"$val $size";

__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-23-2007, 05:44 PM Re: code to show size of specified folder?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
why can't it just be $dirsize['/htdocs/folder'] = $size or somin?... lol

is this how the many control panels for servers etc do?

thanks alot btw.
dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 01-23-2007, 05:57 PM Re: code to show size of specified folder?
Mattmaul1992's Avatar
Ultra Talker

Posts: 486
Name: Matt
Trades: -1
Trust me bud,
Bigger CPanels that people pay a lot of money for I bet are much more complicated then that. That code looks good for what you want, just go along with it.
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 01-23-2007, 06:05 PM Re: code to show size of specified folder?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I'm always happy to help.

I don't know cpanel, never ran it, so I could not say.
It's just that I needed to do that one time, and I always prefer doing my proper coding rather than looking to integrate some code I don't know where it comes from.
And programming is fun, at least for me.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-23-2007, 07:40 PM Re: code to show size of specified folder?
Mattmaul1992's Avatar
Ultra Talker

Posts: 486
Name: Matt
Trades: -1
I was thinking about it later wondering if there might be a more efficient, and maybe at the same time a simpler way. And then I recalled the disk_total_space function.
Here's an example -
PHP Code:
<?php
//=====================
//==== Unix option ====
//=====================
// This shows a KB result I think.
$df disk_total_space("/"); // The "/" is were you enter the directory name.

print $df;

// This shows a MB result.
function du$dir )
{
   
$res = `du -sk $dir`;
   
preg_match'/\d+/'$res$KB ); // Parse result.
   
$MB round$KB[0] / 0);  // From kilobytes to megabytes.
   
return $MB;
}

$dirSize du('/artihelp');
print 
"<br />" $dirSize;

//==========================
//==== Microsoft Option ====
//==========================
disk_total_space("C:");
disk_total_space("D:");
?>
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 01-24-2007, 05:05 AM Re: code to show size of specified folder?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Well, what can I say...
I wasn't aware of the disk_total_space() function.

Looks like a better way than my function.
Thanks for pointing it.

About the second option in your post, mattMaul, the du() function, note that for it to work, the "du" utility must be available for the web server.
In a shared hosting environment, it may not be the case.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-24-2007, 09:14 AM Re: code to show size of specified folder?
Mattmaul1992's Avatar
Ultra Talker

Posts: 486
Name: Matt
Trades: -1
Also it's a good idea to include the disk_free_space function. So what you could do is say you have disck_free_space/disk_total_space available (for example 25/5000 MB). You run the function just like you do with disk_total_space.
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 01-24-2007, 12:54 PM Re: code to show size of specified folder?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
okay when i saved the script it showed 82343243776 told me that it's dividing by zero on line 15 and then showed a 0 under it?...

whats it mean?
i ran it on my windows...

dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 01-24-2007, 01:16 PM Re: code to show size of specified folder?
Veter's Avatar
Super Talker

Posts: 147
Trades: 0
Here is what php manual has for you:

PHP Code:
<?php
function du($dir)
{
   
$du popen("/usr/bin/du -sk $dir""r");
   
$res fgets($du256);
   
pclose($du);
   
$res explode(" "$res);
   
   return 
$res[0];
}
?>
You can find alot of usefull things in manual
__________________
-=
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
=-
Veter is offline
Reply With Quote
View Public Profile Visit Veter's homepage!
 
Old 01-25-2007, 09:51 AM Re: code to show size of specified folder?
Mattmaul1992's Avatar
Ultra Talker

Posts: 486
Name: Matt
Trades: -1
Yup, that's exactly were I went. Everyone, before you ask look up the function name you think the function that you want might have and I bet you'll find it.
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 01-25-2007, 10:23 AM Re: code to show size of specified folder?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
could this do what i want?...

PHP Code:
<?php

// outputs e.g.  somefile.txt: 1024 bytes

$filename 'somefile.txt';
echo 
$filename ': ' filesize($filename) . ' bytes';

?>
</SPAN></DIV>
maybe?

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 01-25-2007, 12:14 PM Re: code to show size of specified folder?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
This will return you the size of somefile.txt, yep.

But for a directory, you must dive into the directory, and summ every files in it.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-25-2007, 01:45 PM Re: code to show size of specified folder?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
okay so could someone do a script which i could enter a directory and it found the size of every file in it and added them together and then make the total equal somin like $dirsize
so i could then display that?

dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 01-25-2007, 04:23 PM Re: code to show size of specified folder?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0


Duh... Look at post n° 2.....
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-25-2007, 04:25 PM Re: code to show size of specified folder?
Mattmaul1992's Avatar
Ultra Talker

Posts: 486
Name: Matt
Trades: -1
This is all over the PHP manual (php.net) Go there and you can have the script ready in 15 or 30 minutes (if you're fast).
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 01-26-2007, 04:20 PM Re: code to show size of specified folder?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
yea but my understanding of php is i supose quite limited.

so the script in post no. 2 would i make a file with that? or what.
im sorry im probally quite annouying. could someone please explain.

Thanks.

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 01-26-2007, 04:56 PM Re: code to show size of specified folder?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Well, you simply make 1 file with the 2 php code block, and you use it by calling

PHP Code:
echo filesize_hum(getDirSize("/home/myusername/www/the/folder/")); 
And it will (should) echo the size of every subdir in GB/MB/KB
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-26-2007, 05:33 PM Re: code to show size of specified folder?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
okay so if i saved those in one folder like
PHP Code:
<?
/**
* Return the byte size of every files and directory from the starting dir given.
* Returns 0 on empty directory. Returns FALSE on an unexisting on unreadable directory
* @param String $path The initial directory path, relative to the file system. Not relative to the web server
* @return Mixed Return FALSE when not a directory or directory not readable, and an Int whith the byte size otherwise
*/
function getDirSize($path){
$ret=false;
 
//We first check the directory, to see if it exists and that we can read it
if( (!is_dir($path)) || (!is_readable($path)) ){
return 
$ret;
}
else{
//There is a directory, if it's empty, his size will be 0
$ret=0;
}
$dirH=opendir($path);
while (
false !== ($file readdir($dirH))) {
if (
$file != "." && $file != "..") {
//We have a file or a directory, and it's not the parent neither itself.
//If it's a directory, we call this function again to get dow it.
if(is_dir($file)){
$ret+=getDirSize($file);
}
else{
//This is a file, we add his size to the rest
$ret+=filesize($file);
}
}
}
closedir($dirH);
 
return 
$ret;
};

/**
* Format a file size in ah human readable way

* @param Int $size The file size
* @return String An human readable converted size
*/
function filesize_hum($size){
$i=0;
$iec = array("B""KB""MB""GB""TB""PB""EB""ZB""YB");
while ((
$size/1024)>1) {
$size=$size/1024;
$i++;
}
$val=round(substr($size,0,strpos($size,'.')+4),2);
$size=$iec[$i];
return 
"$val $size";
}; 
 
echo 
filesize_hum(getDirSize("/home/dan/public_html/")); 

?>
and inserted that into a page it would display the same figure as what it does in my cpanel?

Im going to try it.

Thanks alot.

Dan

__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE

Last edited by dansgalaxy; 01-26-2007 at 05:35 PM..
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 01-26-2007, 05:49 PM Re: code to show size of specified folder?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
It should.
As stated in the post n° 2, I've made it from memory, and have not tested it.

But try it anyway, and post any error you might have.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to code to show size of specified folder?

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.55668 seconds with 12 queries