How to change the maximum file size you can upload to wordpress

Spent a lot of time figuring this out and trying out different things suggested by different sites so thought would make a note of it. Its actually quite simple once you know what you need to do. All you need to do is to change the values of two variables `upload_max_filesize` and `post_max_size` in two places `/etc/php/7.2/apache2/php.ini` and `/etc/php/7.2/cli/php.ini`

$ sudo vi /etc/php/7.2/apache2/php.ini
$ sudo vi /etc/php/7.2/cli/php.ini

we also need to restart the server

$ sudo systemctl restart apache2

Files are uploaded to directory given by [wp_upload_dir](https://developer.wordpress.org/reference/functions/wp_upload_dir/)

To see media files https://wordpress.stackexchange.com/a/95683/29199

“`
Select * from wp_posts where post_type = ‘attachment’;
“`

Troubleshooting: wordpress uses following code to determine the max file size shown in the UI

“`
wp-admin/includes/media.php: $max_upload_size = wp_max_upload_size();
“`

https://developer.wordpress.org/reference/functions/wp_max_upload_size/

in

“`
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 );
}
“`

One tip is that above just controls the value shown in the UI. E.g., you can override the function so that it returns 1G but if the settings in .ini files are not changed, the server (apache) will not let you upload files larger than what the .ini file says.

This entry was posted in Software and tagged . Bookmark the permalink.

Leave a comment