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 Link to comment https://forums.phpfreaks.com/topic/99320-sorry-guys-need-help-again/ 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]; } ?> Link to comment https://forums.phpfreaks.com/topic/99320-sorry-guys-need-help-again/#findComment-508173 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? Link to comment https://forums.phpfreaks.com/topic/99320-sorry-guys-need-help-again/#findComment-508177 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); ?> Link to comment https://forums.phpfreaks.com/topic/99320-sorry-guys-need-help-again/#findComment-508182 Share on other sites More sharing options...
whelpton Posted April 3, 2008 Author Share Posted April 3, 2008 Thanks alot mate Link to comment https://forums.phpfreaks.com/topic/99320-sorry-guys-need-help-again/#findComment-508188 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." Link to comment https://forums.phpfreaks.com/topic/99320-sorry-guys-need-help-again/#findComment-508190 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.