2

Possible Duplicate:
PHP – get the size of a directory

I have 5 files in a directory and it shows file size = 4096 but when i delete 4 files and there is only 1 file left, it still shows the same size. that is how i am getting the file size

echo filesize("user_files/".$uid."/".$rs['DirectoryName']);
Community
  • 1
  • 1
Ali Hassan
  • 39
  • 3

2 Answers2

8

filesize applied to the directory doesn't give the size of its contents. It returns the size of the filesystem node, which, in your case, takes 4kb. This size depends on the amount of the files in the directory.

To get the actual size of contents - you need to get the size of each file and summarize it.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • but if after deleting 3 or 4 files, size of the folder is not 4096 then why its showing the same size? – Ali Hassan Oct 11 '12 at 02:04
  • 1
    @Ali Hassan: because the directory node with size of 4kb can fit hundreds of files. So either it stores 0 or 100 files - it will be anyway of size 4kb – zerkms Oct 11 '12 at 02:06
1

You can get your Total folder size and it Sub directory like this

$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__, FilesystemIterator::SKIP_DOTS));
$size = 0;
foreach ( $dir as $file ) {
    $size += $file->getSize();
}
var_dump($size);

Also note that

Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB.

Baba
  • 94,024
  • 28
  • 166
  • 217