I’m writing a simple file-caching engine in PHP that needs to be able to see how big a directory is. What I’m looking for is the equivalent of Unix’s du command to simply print the total filesize of a directory. I can write it myself, but if somebody else already figured out all the issues of recursing and handling symlinks and so on, that would be great.
I’m writing a simple file-caching engine in PHP that needs to be able to
Share
Something along the lines of this:
The iterator will let you you recursively iterate over the designated folder and subfolders and returns
SplFileInfoobjects. The default option is to just return the leaves, e.g. files, but not directory items, so you can do:By using
getRealPath()on the file, we make sure all symlinks are expanded. Not sure if this would take symlinks to directories into account though. I just don’t know if they are considered leaves then.Anyway, here is a functional approach using a lambda:
And another approach using a custom iterator:
You might want to compare the result to
duto make sure it’s correct.