private_guy Posted September 8, 2007 Share Posted September 8, 2007 Hi there all, How is everyone? Well I was just wondering if somebody got teach me how to make a PHP Script which tells you how much space a folder is taken up? Example: I have a folder called ./files, I want a script which will tell me howmuch space its taken up . Thanks. Best Regards, Chill_Guy Quote Link to comment https://forums.phpfreaks.com/topic/68472-how-much-space-a-folder-is-taken-up/ Share on other sites More sharing options...
Fadion Posted September 8, 2007 Share Posted September 8, 2007 Practically i can think of two ways. The first, clean and simple is disk_total_space(), while the other should be running a recursive function through all files and subfolders of that folder and use filesize(). Anyway the first should do it. Quote Link to comment https://forums.phpfreaks.com/topic/68472-how-much-space-a-folder-is-taken-up/#findComment-344237 Share on other sites More sharing options...
private_guy Posted September 8, 2007 Author Share Posted September 8, 2007 Thanks, but there are sub-folders in this folder. Will the first one this work ? Quote Link to comment https://forums.phpfreaks.com/topic/68472-how-much-space-a-folder-is-taken-up/#findComment-344238 Share on other sites More sharing options...
Fadion Posted September 8, 2007 Share Posted September 8, 2007 Thanks, but there are sub-folders in this folder. Will the first one this work ? U can try it by yourself. It should work normaly. Quote Link to comment https://forums.phpfreaks.com/topic/68472-how-much-space-a-folder-is-taken-up/#findComment-344241 Share on other sites More sharing options...
private_guy Posted September 8, 2007 Author Share Posted September 8, 2007 Hi there again, I cant get it to work, can you show me how to use this function? the Folder i want to disply is ./files. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/68472-how-much-space-a-folder-is-taken-up/#findComment-344258 Share on other sites More sharing options...
wildteen88 Posted September 8, 2007 Share Posted September 8, 2007 disk_total_space does not calulate a size a of a directory. It only calculates the size of the hard drive or a partition within a drive. private_guy I wrote this function a while ago: <?php function folder_size($path, $folder=null) { if(!isset($dir_size)) { $dir_size = 0; } $folder_path = $path . $folder . '/'; $handle = opendir($folder_path); $ignore = array('.', '..'); while ( false !== ($file = readdir($handle))) { if (!in_array($file, $ignore)) { if(!is_dir($folder_path . '/' . $file)) { $dir_size += filesize($folder_path . '/' . $file); } elseif(is_dir($folder_path . '/' . $file)) { $dir_size += folder_size($folder_path, $file); } } } closedir($handle); return $dir_size; } $folder_size = folder_size('./file'); echo 'Total folder size = ' . number_format(($folder_size/1024/1024), 2) . ' MB'; ?> This will calculate the file size of a given directory. Quote Link to comment https://forums.phpfreaks.com/topic/68472-how-much-space-a-folder-is-taken-up/#findComment-344275 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.