Skip to Content
Menu

foldersize()

Add up the filesizes of files in a directory (and it’s sub-directories)

PHP April 27, 2017

Usage

PHP
nebula()->foldersize($path)

Parameters

$path
(Required) (String) The path to the directory to add
Default: None

Request or provide clarification »

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Utilities/Utilities.php on line 982.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "pre_foldersize"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    PHP
            public function foldersize($path){
                $override = apply_filters('pre_foldersize', null, $path);
                if ( isset($override) ){return $override;}
    
                $total_size = 0;
                $files = scandir($path);
                $cleanPath = rtrim($path, '/') . '/';
                foreach ( $files as $file ){
                    if ( $file <> '.' && $file <> '..'){
                        $currentFile = $cleanPath . $file;
                        if ( is_dir($currentFile) ){
                            $size = $this->foldersize($currentFile);
                            $total_size += $size;
                        } else {
                            $size = filesize($currentFile);
                            $total_size += $size;
                        }
                    }
                }
    
                return $total_size; //Return total size in bytes
            }
    

    Override

    To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

    PHP
    add_filter('pre_foldersize', 'my_custom_foldersize', 10, 2); //The last integer must be 1 more than the actual parameters
    function my_custom_foldersize($null, $path){ //$null is required, but can be ignored
        //Write your own code here
    
        return true; //Return true to prevent the original function from running afterwords
    }

    You can completely disable this PHP function with a single line actions:

    PHP
     add_filter('pre_foldersize', '__return_false');