get_dirsize( string $directory, int $max_execution_time = null )

Get the size of a directory.


Description Description

A helper function that is used primarily to check whether a blog has exceeded its allowed upload space.


Top ↑

Parameters Parameters

$directory

(string) (Required) Full path of a directory.

$max_execution_time

(int) (Optional) Maximum time to run before giving up. In seconds. The timeout is global and is measured from the moment WordPress started to load.

Default value: null


Top ↑

Return Return

(int|false|null) Size in bytes if a valid directory. False if not. Null if timeout.


Top ↑

Source Source

File: wp-includes/functions.php

function get_dirsize( $directory, $max_execution_time = null ) {

	// Exclude individual site directories from the total when checking the main site of a network,
	// as they are subdirectories and should not be counted.
	if ( is_multisite() && is_main_site() ) {
		$size = recurse_dirsize( $directory, $directory . '/sites', $max_execution_time );
	} else {
		$size = recurse_dirsize( $directory, null, $max_execution_time );
	}

	return $size;
}


Top ↑

Changelog Changelog

Changelog
Version Description
MU (3.0.0) MU (3.0.0)
5.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Naveen Kharwar

    To echo the WordPress directory size.

    <?php
    if ( ! function_exists( 'get_dirsize' ) ) {
    	require_once ABSPATH . WPINC . '/ms-functions.php';
    }
    
    // Get the path of a directory.
    $directory = get_home_path();
    
    // Get the size of directory in bytes.
    $result = get_dirsize( $directory );
    
    echo number_format( $result / ( 1024 * 1024 ), 1 ) . ' MB'; // example output: 43.6 MB
    ?>

You must log in before being able to contribute a note or feedback.