Jump to content

Sorry guys, Need help again =)


whelpton

Recommended Posts

Sorry about posting so much. I recently got the code to show how much space a folder is using:

 

<?php

$path = "gal";

echo "Folder $path = ".filesize_r($path)." bytes";

 

function filesize_r($path){

  if(!file_exists($path)) return 0;

  if(is_file($path)) return filesize($path);

  $ret = 0;

  foreach(glob($path."/*") as $fn)

    $ret += filesize_r($fn);

  return $ret;

}

?>

 

Is there anyway to convert this to show MegaBytes instead of Bytes?

 

Thanks

Link to comment
Share on other sites

You could run your final filesize value through this function. (from: http://us2.php.net/filesize)

 

<?php 
function format_size($size, $round = 0) { 
    //Size must be bytes! 
    $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); 
    for ($i=0; $size > 1024 && $i < count($sizes) - 1; $i++) $size /= 1024; 
    return round($size,$round).$sizes[$i]; 
} 
?>  

 

[...] in-case someone doesn't want it to go up to 'TB' and deletes 'TB' onwards but the file size is larger than 1024GB this checks to see if you have outreached the size of $sizes

 

KiloBytes, MegaBytes and Gigabytes only.

<?php 
function format_size($size, $round = 0) { 
    //Size must be bytes! 
    $sizes = array('B', 'kB', 'MB', 'GB'); 
    for ($i=0; $size > 1024 && $i < count($sizes) - 1; $i++) $size /= 1024; 
    return round($size,$round).$sizes[$i]; 
} 
?>  

Link to comment
Share on other sites

No Problem.  Hope this clarifies.

 

<?php 
function format_size($size, $round = 0) { 
    //Size must be bytes! 
    $sizes = array('B', 'kB', 'MB', 'GB'); 
    for ($i=0; $size > 1024 && $i < count($sizes) - 1; $i++) $size /= 1024; 
    return round($size,$round).$sizes[$i]; 
} 

function filesize_r($path){
  if(!file_exists($path)) return 0;
  if(is_file($path)) return filesize($path);
  $ret = 0;
  foreach(glob($path."/*") as $fn)
    $ret += filesize_r($fn);
  return $ret;
}

$path = "gal";
$raw_filesize = filesize_r($path);
echo "Folder $path = ".format_size($raw_filesize);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.