/** * * Return size of path directory using recursive trace * @param string $directory * @param boolean $format */ function directory_size($directory, $format = false) { $size = 0; // if the path has a slash at the end we remove it here if (substr($directory, -1) == '/') { $directory = substr($directory, 0, -1); } // if the path is not valid or is not a directory ... if (!file_exists($directory) || !is_dir($directory) || !is_readable($directory)) { return -1; } // we open the directory if ($handle = opendir($directory)) { // and scan through the items inside while (($file = readdir($handle)) !== false) { // we build the new path $path = $directory . '/' . $file; // if the filepointer is not the current directory // or the parent directory if ($file != '.' && $file != '..') { // if the new path is a file, add the filesize to the total size if (is_file($path)) { $size += filesize($path); } elseif (is_dir($path)) { // we call this function with the new path $handlesize = directory_size($path); // if the function returns more than zero, we add the result to the total size if ($handlesize >= 0) { $size += $handlesize; } else { //else we return -1 and exit the function return -1; } } } } // close the directory closedir($handle); } // if the format is set to human readable if ($format == true) { if ($size / 1048576 > 1) { return round($size / 1048576, 1) . ' MB'; } elseif ($size / 1024 > 1) { return round($size / 1024, 1) . ' KB'; } else { return round($size, 1) . ' bytes'; } } else { return $size; } }
function getUserSize($username, $basedir, $justsize = false) { global $mysqlpre; $sizequery = "SELECT quota FROM " . $mysqlpre . "_users WHERE username = '******'"; $sizeresult = mysql_query($sizequery); $totalsize = mysql_fetch_row($sizeresult); if (!$justsize) { $returndata = round(directory_size($basedir . $username) / 1024 / 1024, 2) . "MB of " . $totalsize[0] . "MB"; return $returndata; } else { $returndata[] = round(directory_size($basedir . $username) / 1024 / 1024, 2); $returndata[] = $totalsize[0]; return $returndata; } }
function directory_size($directory) { $directorySize = 0; if ($dh = @opendir($directory)) { while ($filename = readdir($dh)) { if ($filename != "." && $filename != "..") { if (is_file($directory . "/" . $filename)) { $directorySize += filesize($directory . "/" . $filename); } if (is_dir($directory . "/" . $filename)) { $directorySize += directory_size($directory . "/" . $filename); } } } } @closedir($dh); return $directorySize; }
function directory_size($dir) { $byte_size = 0; $dir_array = scandir($dir); foreach ($dir_array as $key => $filename) { if ($filename != ".." && $filename != ".") { if (is_dir($dir . "/" . $filename)) { $new_foldersize = directory_size($dir . "/" . $filename); $byte_size = $byte_size + $new_foldersize; } else { if (is_file($dir . "/" . $filename)) { $byte_size = $byte_size + filesize($dir . "/" . $filename); } } } } return $byte_size; }