SFTP: add optional $recursive parameter to filesize()

This commit is contained in:
terrafrost 2023-06-04 10:29:52 -05:00
parent f418be845b
commit b8f8f0b7db
1 changed files with 24 additions and 2 deletions

View File

@ -2829,15 +2829,37 @@ class SFTP extends SSH2
return $this->get_stat_cache_prop($path, 'gid');
}
/**
* Recursively go through rawlist() output to get the total filesize
*
* @return int
*/
private static function recursiveFilesize(array $files)
{
$size = 0;
foreach ($files as $name => $file) {
if ($name == '.' || $name == '..') {
continue;
}
$size+= is_array($file) ?
self::recursiveFilesize($file) :
$file->size;
}
return $size;
}
/**
* Gets file size
*
* @param string $path
* @param bool $recursive
* @return mixed
*/
public function filesize($path)
public function filesize($path, $recursive = false)
{
return $this->get_stat_cache_prop($path, 'size');
return !$recursive || $this->filetype($path) != 'dir' ?
$this->get_stat_cache_prop($path, 'size') :
$this->recursiveFilesize($this->rawlist($path, true));
}
/**