国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 南康市| 保亭| 南城县| 灵丘县| 蒙城县| 潞城市| 刚察县| 衡水市| 商水县| 禄劝| 喀喇沁旗| 东宁县| 广灵县| 尼勒克县| 临猗县| 皋兰县| 水城县| 恩平市| 寿宁县| 苏尼特右旗| 林州市| 平泉县| 宽城| 丹江口市| 左权县| 峨边| 达日县| 宣威市| 纳雍县| 贵阳市| 敦煌市| 南涧| 大渡口区| 弥勒县| 五华县| 卓资县| 汾西县| 肇东市| 泰和县| 会理县| 湟源县|