whelpton Posted April 3, 2008 Share Posted April 3, 2008 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 Quote Link to comment Share on other sites More sharing options...
devstudio Posted April 3, 2008 Share Posted April 3, 2008 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]; } ?> Quote Link to comment Share on other sites More sharing options...
whelpton Posted April 3, 2008 Author Share Posted April 3, 2008 Sorry im a newbie to php, just how would I Run it though the second function? Quote Link to comment Share on other sites More sharing options...
devstudio Posted April 3, 2008 Share Posted April 3, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
whelpton Posted April 3, 2008 Author Share Posted April 3, 2008 Thanks alot mate Quote Link to comment Share on other sites More sharing options...
devstudio Posted April 3, 2008 Share Posted April 3, 2008 No Problem. If it's working please mark this thread as "Solved." Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.