国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

php如何使用遞歸來(lái)計(jì)算一個(gè)目錄中所有文件的大

2020-03-22 19:30:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
本篇文章給大家?guī)?lái)的內(nèi)容是關(guān)于php如何使用遞歸來(lái)計(jì)算一個(gè)目錄中所有文件的大小 (代碼),有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你有所幫助。

sudo find /private/etc -exec ls -l {} /; | awk BEGIN {sum=0} {sum+=$5;} END {print sum} # 4947228ls -ld /etc #/etc - private/etc

先計(jì)算出/etc目錄所有文件的大小4947228

DirUtil.php

 ?php * Created by PhpStorm. * User: Mch * Date: 8/14/18 * Time: 22:11html' target='_blank'>class DirUtil { public static function getSize(string $path) { $totalSize = 0; $path = realpath($path); if (!file_exists($path)) { return $totalSize; if (!is_dir($path)) { return filesize($path); if ($dh = opendir($path)) { while (($file = readdir($dh)) !== false) { if ($file !== . $file !== .. ) { $abs = $path.DIRECTORY_SEPARATOR.$file; if (is_dir($file)) { $totalSize += self::getSize($abs); } else { $totalSize += filesize($abs); closedir($dh); return $totalSize; public static function entryForEach(string $path, callable $callback, mixed $data = null) { $path = realpath($path); if (!file_exists($path)) { return 0; if (!is_dir($path)) { return call_user_func($callback, $path, $data); if ($dh = opendir($path)) { while (($file = readdir($dh)) !== false) { if ($file !== . $file !== .. ) { $abs = $path.DIRECTORY_SEPARATOR.$file; if (is_dir($file)) { self::entryForEach($abs, $callback, $data); } else { call_user_func($callback, $abs, $data); closedir($dh); return 0; public static function entryReduce(string $path, callable $callback, $init) { $acc = $init; $path= realpath($path); if (!file_exists($path)) { return $acc; if (!is_dir($path)) { return call_user_func($callback, $acc, $path); if ($dh = opendir($path)) { while (($file = readdir($dh)) !== false) { if ($file !== . $file !== .. ) { $abs = $path.DIRECTORY_SEPARATOR.$file; if (is_dir($file)) { $acc = self::entryReduce($abs, $callback, $acc); } else { $acc= call_user_func($callback, $acc, $abs); closedir($dh); return $acc;}

test:

// php ./DirUtil.php /etcif ($argc 2) { printf( Usage: php %s [filename]/n , __FILE__); exit(1);echo DirUtil::getSize($argv[1]).PHP_EOL; // 899768$dir_get_size = function($path) { $size = 0; DirUtil::entryForEach($path, function($path) use ( $size) { $size += filesize($path); return $size;echo $dir_get_size($argv[1]).PHP_EOL; // 899768echo DirUtil::entryReduce($argv[1], function($sum, $path) { $sum += filesize($path); return $sum;}, 0).PHP_EOL; // 899768

相關(guān)推薦:

php遞歸示例 php遞歸函數(shù)代碼

PHP遞歸創(chuàng)建多級(jí)目錄,php遞歸

php遞歸json類實(shí)例,php遞歸json_PHP教程

以上就是php如何使用遞歸來(lái)計(jì)算一個(gè)目錄中所有文件的大小 (代碼)的詳細(xì)內(nèi)容,PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 都匀市| 镇沅| 玛曲县| 农安县| 岑溪市| 泸水县| 象山县| 临城县| 龙山县| 常宁市| 洱源县| 青浦区| 康马县| 麦盖提县| 河曲县| 蒲江县| 丁青县| 大荔县| 加查县| 老河口市| 卓资县| 循化| 隆尧县| 蓬安县| 凌源市| 无棣县| 永靖县| 闽清县| 镇赉县| 乡城县| 健康| 额济纳旗| 同江市| 昆山市| 海阳市| 十堰市| 喀喇沁旗| 青州市| 成安县| 吉首市| 城口县|