Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get the filesize of all the files in a folder!

Status
Not open for further replies.

MJB3K

Programmer
Jul 16, 2004
524
GB
Hi, how would you open a folder, then get the filesize for each file, add them together and then display the total file size??

any ideas??


Regards,

Martin

Gaming Help And Info:
 
If you are in a *NIX type OS you could also use the system specific du function in an exec() statement.
 
1: Find a VERY simple dirlist script
2: In the loop, add a line:
Code:
$total = filesize($the_variable_that_contains_file);
3: parse it out

ps. you might need to run clearstatcache() before each time you run the filesize(), as the results of filesize() are cached!

ref.: ttp://us2.php.net/manual/en/function.clearstatcache.php

For the simple dirlist script, look here:

Olav Alexander Mjelde
Admin & Webmaster
 
I guess it's your lucky day.. I was a bit bored now, so I made it for you..

Code:
<?php
$dir = "./";

echo "<table width=\"100%\">
<tr><td>Filename</td><td>FileSize</td><td>Filetype</td></tr>
<tr><td colspan=\"3\"><hr /></td></tr>
";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           $fsize = filesize($dir . $file); // get filesize
           echo "<tr><td>{$file}</td><td>{$fsize}</td><td>" . filetype($dir . $file) . "</td></tr>\n";
          $numfiles++;
          $sum_kb += $fsize;
        clearstatcache(); // clear cache about files..
       }
       closedir($dh);
       echo "<tr><td colspan=\"3\"><hr />Total filesize: {$sum_kb}<br />Number of files: {$numfiles}</td></tr>
                </table>";
   }
}
?>

ps. you might want to do something with the way filesize is displayed. I hardly think anyone wants to see it in kb.. unless you specify that your users can only upload 512kb or something like that though.

bare in mind that 1mb = 1024kb, not 1000..
This is why you dont get 250gb out of a 250gb harddrive. it's actually ((250 * 1000) * (1 / 1.024)) / 1000
results in 244GB 140MB and 625KB

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top