Alt dizindeki klasörlerdeki dosya sayıları ve toplam olarak kaç TB olduğunu gösteren class ve fonksiyon.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
<?php class FileFinder { private $onFound; private function __construct($path, $onFound, $maxDepth) { // onFound gets called at every file found $this->onFound = $onFound; // start iterating immediately $this->iterate($path, $maxDepth); } private function iterate($path, $maxDepth) { $d = opendir($path); while ($e = readdir($d)) { // skip the special folders if ($e == '.' || $e == '..') { continue; } $absPath = "$path/$e"; if (is_dir($absPath)) { // check $maxDepth first before entering next recursion if ($maxDepth != 0) { // reduce maximum depth for next iteration $this->iterate($absPath, $maxDepth - 1); } } else { // regular file found, call the found handler call_user_func_array($this->onFound, array($absPath)); } } closedir($d); } // helper function to instantiate one finder object // return value is not very important though, because all methods are private public static function find($path, $onFound, $maxDepth = 0) { return new self($path, $onFound, $maxDepth); } } // start finding files (maximum depth is one folder down) $count = $bytes = 0; FileFinder::find('.', function($file) use (&$count, &$bytes) { // the closure updates count and bytes so far ++$count; $bytes += filesize($file); }, 1); function byteFormat($bytes, $unit = "", $decimals = 2) { $units = array('B' => 0, 'KB' => 1, 'MB' => 2, 'GB' => 3, 'TB' => 4, 'PB' => 5, 'EB' => 6, 'ZB' => 7, 'YB' => 8); $value = 0; if ($bytes > 0) { // Generate automatic prefix by bytes // If wrong prefix given if (!array_key_exists($unit, $units)) { $pow = floor(log($bytes)/log(1024)); $unit = array_search($pow, $units); } // Calculate byte value by prefix $value = ($bytes/pow(1024,floor($units[$unit]))); } // If decimals is not numeric or decimals is less than 0 // then set default value if (!is_numeric($decimals) || $decimals < 0) { $decimals = 2; } // Format output return sprintf('%.' . $decimals . 'f '.$unit, $value); } $kactb = byteFormat($bytes, "TB"); echo "Toplam Dosya: $count; </br> Ne kadar tutuyor bu: $kactb\n"; ?> |
byteformat değerine GB,MB yazarak output sonucunu Gigabyte,Megabyte cinsinden gösterebilirsiniz.