Using WordPress ‘get_upload_space_available()’ PHP function

The get_upload_space_available() WordPress PHP function determines if there is any upload space left in the current blog’s quota.

Usage

$available_space = get_upload_space_available();

Parameters

  • None

More information

See WordPress Developer Resources: get_upload_space_available()

Examples

Check if there is enough space to upload a file

Determine if there is enough space available to upload a file of a given size.

$file_size = 1000000; // 1MB in bytes

$available_space = get_upload_space_available();

if ($file_size <= $available_space) {
    // Proceed with file upload
} else {
    // Display an error message
}

Display available upload space in human-readable format

Show the available upload space in a human-readable format.

$available_space = get_upload_space_available();
$available_space_human = size_format($available_space);

echo "You have {$available_space_human} of upload space available.";

Limit the max file upload size

Set the maximum file upload size based on available upload space.

$available_space = get_upload_space_available();
$max_file_size = min($available_space, wp_max_upload_size());

// Use $max_file_size as the maximum allowed file size for uploads

Display a message if no upload space is available

Show a message to the user if there is no upload space left.

$available_space = get_upload_space_available();

if ($available_space <= 0) {
    echo "No upload space left. Please delete some files to free up space.";
}

Add a warning when upload space is low

Display a warning message when available upload space is below a specified threshold.

$available_space = get_upload_space_available();
$threshold = 5000000; // 5MB in bytes

if ($available_space < $threshold) {
    echo "Warning: You have less than 5MB of upload space left.";
}