php如何使用遞迴來計算一個目錄中所有檔案的大小 (代碼)

來源:互聯網
上載者:User
本篇文章給大家帶來的內容是關於php如何使用遞迴來計算一個目錄中所有檔案的大小 (代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所協助。

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

先計算出/etc目錄所有檔案的大小4947228

DirUtil.php

<?php/** * Created by PhpStorm. * User: Mch * Date: 8/14/18 * Time: 22:11 */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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.