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? Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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); } ?> Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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)))); } ?> 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.