2
echo $sftp->size('foldername');

This code only return the default size of directory 4096

How can i read the total size, used space of the directory/folder that contains many files?

Thanks a lot for your help!

1 Answers1

3

To do this with SFTP I guess you could do something like this (untested):

$size = 0;
$files = $sftp->rawlist('foldername', true);
foreach ($files as $file) {
    $size+= $file['size'];
}

Note that recursive directory listings with SFTP can be slow.

Alternatively, you could do $sftp->exec('du -s foldername');. That'd be the fastest way. Since SFTP extends SSH2 with phpseclib exec is a valid method.

neubert
  • 15,947
  • 24
  • 120
  • 212
  • 1
    +1 - Though worth noting that the `$sftp->exec('du -s foldername')` is not SFTP solution anymore. It requires a shell access and is *nix specific. – Martin Prikryl Sep 22 '17 at 06:46