Last active
March 3, 2020 16:43
-
-
Save greg-randall/58bddca2a6fd6f7bce6931976d74c75e to your computer and use it in GitHub Desktop.
Prints out a table with all the directories on a web server. Good for chasing down large files during a cleanup. Bytes column is for easy sorting in excel if needed. If you have a lot of files it will be slow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$root = '.'; | |
$min_size = 10485760; | |
/* in bytes. | |
1 mb = 1048576 | |
10 mb = 10485760 | |
100 mb = 104857600 | |
1 gb = 1048576000 | |
*/ | |
$iter = new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS), | |
RecursiveIteratorIterator::SELF_FIRST, | |
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied" | |
); | |
$paths = array( | |
$root | |
); | |
echo "Minimum File Size: ". format_size($min_size) ."<br>"; | |
?> | |
<table border="1"> | |
<tr><th>Bytes</th><th>Size</th><th>Directory</th></tr> | |
<?php | |
foreach ($iter as $path => $dir) { | |
if ($dir->isDir()) { | |
$dir_size = get_dir_size($path); | |
if($dir_size>$min_size){ | |
$folders[$dir_size] = "<tr><td>$dir_size</td><td>" . format_size($dir_size) . "</td><td>$path</td></tr>\n"; | |
} | |
} | |
} | |
krsort($folders); | |
foreach ($folders as $folder) { | |
echo $folder; | |
} | |
function get_dir_size($directory) { | |
$size = 0; | |
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { | |
$size += $file->getSize(); | |
} | |
return $size; | |
} | |
function format_size($size) {//https://stackoverflow.com/posts/2510459/revisions | |
if ($size >= 1) { | |
$si_prefix = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ); | |
$base = 1024; | |
$class = min((int) log($size, $base), count($si_prefix) - 1); | |
return (sprintf('%1.2f', $size / pow($base, $class)) . $si_prefix[$class]); | |
} else { | |
return (0); | |
} | |
} | |
?> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment