Created
July 9, 2013 05:32
-
-
Save mexitek/5954939 to your computer and use it in GitHub Desktop.
Run this PHP script via command line and see all the directories with contents over 1 KB.
This file contains hidden or 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 | |
// Base directory is where you run the script from | |
$currentDirectory = '.'; | |
// Get all contents, minus pointers to . and .. | |
$baseLevelContents = array_diff( scandir( $currentDirectory ), array('..','.') ); | |
// All directories | |
$baseDirs = array_filter( $baseLevelContents, "is_dir" ); | |
// All base files | |
$baseFiles = array_diff( $baseLevelContents, $baseDirs ); | |
// Iterate through the all the directories | |
foreach( $baseDirs as $dir ) { | |
// Recursively get the total file sizes | |
$totalFileSize = getFileSizeSum( $dir ); | |
// If exceeds limit, do something | |
if( $totalFileSize/1024 > 1 ) { | |
echo $dir."/ seems to contain ".number_format($totalFileSize/1024)." KB of data."; | |
} | |
} | |
// Method that returns the sum of all file sizes | |
function getFileSizeSum( $dir ) { | |
$sum = 0; | |
// Get all contents, minus pointers to . and .. | |
$contents = array_diff( scandir( $dir ), array('..','.') ); | |
// All directories | |
$dirs = array_filter( $contents, "is_dir" ); | |
// All base files | |
$files = array_diff( $contents, $dirs ); | |
// Sum all the files | |
foreach( $files as $file ) { | |
$sum += filesize( $dir."/".$file ); | |
} | |
// Recusively call all sub directories | |
foreach( $dirs as $subDir ) { | |
$sum += getFileSizeSum( $dir."/".$subDir ); | |
} | |
// Return the sum | |
return $sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment