Using WordPress ‘get_dirsize()’ PHP function

The get_dirsize() WordPress PHP function retrieves the size of a directory.

Usage

To use the function to get the size of a specific directory:

$directory = '/path/to/directory';
$dir_size = get_dirsize($directory);

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: null

More information

See WordPress Developer Resources: get_dirsize

Examples

Display the size of the WordPress root directory

This example retrieves the size of the WordPress root directory and displays it in megabytes (MB).

// Get the path of the WordPress root directory
$directory = ABSPATH;

// Get the size of the directory in bytes
$dir_size = get_dirsize($directory);

// Display the size in MB
echo number_format($dir_size / (1024 * 1024), 1) . ' MB'; // Output: 43.6 MB

Get the size of the uploads directory

This example retrieves the size of the WordPress uploads directory.

// Get the path of the uploads directory
$upload_dir = wp_upload_dir();
$directory = $upload_dir['basedir'];

// Get the size of the directory
$dir_size = get_dirsize($directory);

Calculate the percentage of used upload space

This example calculates the percentage of used upload space for a blog.

// Get the allowed upload space in MB
$allowed_space = get_space_allowed();

// Convert allowed space to bytes
$allowed_space_bytes = $allowed_space * 1024 * 1024;

// Get the path of the uploads directory
$upload_dir = wp_upload_dir();
$directory = $upload_dir['basedir'];

// Get the size of the directory
$used_space = get_dirsize($directory);

// Calculate the percentage of used space
$used_percentage = ($used_space / $allowed_space_bytes) * 100;

Check if the blog has exceeded its allowed upload space

This example checks if a blog has exceeded its allowed upload space and displays a message accordingly.

// Get the allowed and used upload space
$allowed_space = get_space_allowed();
$used_space = get_upload_space_used();

// Check