From b8f8f0b7dba76bece84786f504f9a04b31d75235 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 4 Jun 2023 10:29:52 -0500 Subject: [PATCH] SFTP: add optional $recursive parameter to filesize() --- phpseclib/Net/SFTP.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index dd26824a..2de4b520 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -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)); } /**