Skip to content

Instantly share code, notes, and snippets.

@mocanuga
Last active April 4, 2017 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mocanuga/77a0fdc5ede17eefed51f0b328ae9763 to your computer and use it in GitHub Desktop.
Save mocanuga/77a0fdc5ede17eefed51f0b328ae9763 to your computer and use it in GitHub Desktop.
How to return the size of directory even if the directory is symlink with PHP?
<?php
/**
* @author mocanuga
* @desc Return the size of directory in bytes. Tested on CentOS 5.11 Final
* @return int
*/
function getFolderSize($folder = '') {
if(empty($folder))
return -1;
$dir = escapeshellcmd($folder);
if(!is_dir($dir))
return -1;
$cmd = is_link($dir) ? `du -sb $(readlink -f $dir)` : `du -sb $dir`;
$res = explode("\t", $cmd);
return (int)array_shift($res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment