Optimized and refactored fm_get_directorysize() function, and added validation. (#720)

This commit is contained in:
Michael Milette 2022-02-12 12:45:38 -05:00 committed by GitHub
parent 7103691048
commit 9b2bb18acb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2573,24 +2573,20 @@ function fm_get_filesize($size)
} }
/** /**
* Get director total size * Get total size of directory tree.
* @param string $directory *
* @return int * @param string $directory Relative or absolute directory name.
* @return int Total number of bytes.
*/ */
function fm_get_directorysize($directory) { function fm_get_directorysize($directory) {
global $calc_folder; $bytes = 0;
if ($calc_folder==true) { // Slower output $directory = realpath($directory);
$size = 0; $count= 0; $dirCount= 0; if ($directory !== false && $directory != '' && file_exists($directory)){
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS)) as $file){
if ($file->isFile()) $bytes += $file->getSize();
{ $size+=$file->getSize();
$count++;
} }
else if ($file->isDir()) { $dirCount++; }
// return [$size, $count, $dirCount];
return $size;
} }
else return 'Folder'; // Quick output return $bytes;
} }
/** /**