Dysan Posted February 20, 2008 Share Posted February 20, 2008 Hi, How do I open a directory using opendir(), loop through the files, and depending on the size of the folder, display the total size in KB, MB, GB, TB etc? Link to comment https://forums.phpfreaks.com/topic/92173-total-size/ Share on other sites More sharing options...
schilly Posted February 20, 2008 Share Posted February 20, 2008 quick google search: http://www.phpit.net/code/list-files-directory/ then just use filesize() and divide by your binary increments to get KB, MB, GB, etc. Link to comment https://forums.phpfreaks.com/topic/92173-total-size/#findComment-472234 Share on other sites More sharing options...
Dysan Posted February 21, 2008 Author Share Posted February 21, 2008 Could you give me an example, I don't quite understand? Link to comment https://forums.phpfreaks.com/topic/92173-total-size/#findComment-473189 Share on other sites More sharing options...
schilly Posted February 21, 2008 Share Posted February 21, 2008 <?php //from link with modifications if ($handle = opendir('/path/to/files')) { echo "Directory handle: $handle\n"; echo "Files:\n"; // List all the files while (false !== ($file = readdir($handle))) { echo "$file size is: " . filesize($file) . "\n"; //divide filesize by 1024 for kb, 1024^2 for mb, 1024^3 for gb, etc } closedir($handle); } ?> Link to comment https://forums.phpfreaks.com/topic/92173-total-size/#findComment-473215 Share on other sites More sharing options...
duclet Posted February 21, 2008 Share Posted February 21, 2008 You can also use the function disk_total_space to get the filesize of a folder so you don't need to do all those filesize on all those files. http://us3.php.net/manual/en/function.disk-total-space.php Link to comment https://forums.phpfreaks.com/topic/92173-total-size/#findComment-473216 Share on other sites More sharing options...
Dysan Posted February 25, 2008 Author Share Posted February 25, 2008 Can you give me an example of what you mention in your previous most (above) as I have tried it, but I failed to get it working. Link to comment https://forums.phpfreaks.com/topic/92173-total-size/#findComment-475465 Share on other sites More sharing options...
Bauer418 Posted February 25, 2008 Share Posted February 25, 2008 Taken from the comments on PHP.net (modified slightly): <?php function getSymbolByQuantity($bytes) { $symbols = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'); $exp = $bytes ? floor(log($bytes) / log(1024)) : 0; return sprintf('%.2f ' . $symbols[$exp], ($bytes / pow(1024, floor($exp)))); } ?> Link to comment https://forums.phpfreaks.com/topic/92173-total-size/#findComment-475469 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.