wp_max_upload_size()WP 2.5.0

Determines the maximum upload size allowed in php.ini.

1 time — 0.0000138 sec (very fast) | 50000 times — 0.05 sec (speed of light)
Hooks from the function

Return

Int. Allowed upload size.

Usage

wp_max_upload_size();

Examples

0

#1 Limiting the file size for your form

Suppose we have such a form where we have added the maximum allowed file size to the data-maxfsize attribute:

<form>
  <input id="file-input" type="file" data-maxfsize="<?= (int) wp_max_upload_size() ?>" />
  <p id="file-result"></p>
  <input id="file-submit" type="submit" disabled />
</form>

Now, we can use a JS script like this to check a file for its size:

const fileInput = document.getElementById('file-input')
const fileResult = document.getElementById('file-result')
const fileSubmit = document.getElementById('file-submit')

const maxFileSize = parseInt( fileInput.dataset['maxfsize'] )

fileInput.addEventListener( 'change', function(){  

	if( fileInput.files.length > 0 ){
		const fileSize = fileInput.files.item(0).size

		if( maxFileSize >= fileSize ){
			fileSubmit.disabled = false
		}
		else{
			fileResult.innerHTML = `Please select a file less than ${ Math.floor( maxFileSize / (1024 * 1024) ) } MB.`
		}
	}

} );
0

#2 Limit the maximum file size that can be uploaded

Let's say our server allows us to upload files that are 300 MB in size. We need this for other scripts, but in WordPress we want managers to not be able to upload files larger than 10 MB.

To achieve this, we can use the upload_size_limit filter:

add_filter( 'upload_size_limit', static fn() => MB_IN_BYTES * 10 );
0

#3 What does the function return

var_dump( wp_max_upload_size() ); //> int(10485760)

Changelog

Since 2.5.0 Introduced.

wp_max_upload_size() code WP 6.4.3

function wp_max_upload_size() {
	$u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) );
	$p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) );

	/**
	 * Filters the maximum upload size allowed in php.ini.
	 *
	 * @since 2.5.0
	 *
	 * @param int $size    Max upload size limit in bytes.
	 * @param int $u_bytes Maximum upload filesize in bytes.
	 * @param int $p_bytes Maximum size of POST data in bytes.
	 */
	return apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes );
}