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
https://forums.phpfreaks.com/topic/99320-sorry-guys-need-help-again/
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]; 
} 
?>  

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);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.