1

The current file upload size limit is 128MB. I want to reduce this to 1MB. I do not have access to php.ini file. I do have access to .htaccess and I could use this solution.

But why isn't there a method for doing this in the functions.php file?

** UPDATE **

So this, over at Stackoverflow, basically says why:

Those settings are not going to have any effect when set via ini_set

The reason is that PHP needs those values before your script is even executed. When an upload occurs, the target script is executed when the upload is complete, so PHP needs to know the maximum sizes beforehand.

Set them in php.ini, your virtual host config, or in a .htaccess file. A typical .htaccess file would look like this

php_value post_max_size 30M
php_value upload_max_filesize 30M

So the question remains; Is there a way to set max upload size in WP functions.php?

6
  • check stackoverflow.com/questions/13442270/… and you will understand why Commented Oct 2, 2014 at 10:45
  • you can add to functions: @ini_set( 'upload_max_size' , '64M' ); @ini_set( 'post_max_size', '64M'); @ini_set( 'max_execution_time', '300' );
    – Hoang An
    Commented Oct 2, 2014 at 10:48
  • I get that. But WP is using Plupload, right? So why isn't there a setting in WP where I can set upload size? (where max is according to php.ini)
    – Steven
    Commented Oct 2, 2014 at 12:48
  • Are you asking how to set a file size limit for uploads via your functions.php file? If yes, then please do a search here because this has already been answered previously. Nonetheless, I posted the solution for you.
    – Christine Cooper
    Commented Oct 2, 2014 at 21:36
  • I always google. If I can't find the answer, I come here :)
    – Steven
    Commented Oct 3, 2014 at 8:41

3 Answers 3

4

Absolutely, you can simply hook into upload_size_limit and set a maximum filesize:

// Change the upload size to 1MB
add_filter( 'upload_size_limit', 'wpse_163236_change_upload_size' ); 
function wpse_163236_change_upload_size()
{
    return 1000 * 1024;
}
2

You can restrict the file size via the wp_handle_upload_prefilter, where

The single parameter, $file, represent a single element of the $_FILES array.

The code below is just exemplary, but you'll get the point.

add_filter(
    'wp_handle_upload_prefilter',
    'wpse163236_restrict_upload_file_size'
);
function wpse163236_restrict_upload_file_size( $file ){
    $file_size  = $file[ 'size' ];
    $size_limit = '1024000';
    if ( $size_limit < $file_size ) {
        $file[ 'error' ] = 'File Size Over: ' . $size_limit;
    }
    return $file;
}
2
  • 1
    Hooking into 'upload_size_limit' would work just as well, no?
    – Christine Cooper
    Commented Oct 2, 2014 at 21:39
  • 1
    @ChristineCooper Actually I just forgot upload_size_limit, never use it, it's easier and clearer, definitely preferable. I've only chosen wp_handle_upload_prefilter, because I use it sometimes for some naming stuff and so it came to my mind. Commented Oct 2, 2014 at 22:12
0

No there isn't a way to reduce the file size without access to php.ini or .htaccess for the reason your quote explains very well.

The best you can do without access to php.ini is to ignore the file as lalocin's answer shows

3
  • 1
    I am confused by some answers here. Isn't Steven simply asking how to limit the upload size limit? This can be done easily by hooking into upload_size_limit...
    – Christine Cooper
    Commented Oct 2, 2014 at 21:34
  • 3
    @ChristineCooper It depends what he actually wants to avoid. For most of what I can think of using the filter is good enough but if you need to reduce the incoming bandwidth it is not a solution since the whole file will be uploaded and then discarded wasting your bandwidth. OTOH A php.ini based solution will abort the upload once the server knows the file is bigger then the limit and waste bandwidth of only 1M instead of the possible 128M in his case. Commented Oct 3, 2014 at 2:40
  • Right right, interesting. So hooking into this filter will only affect the file once it has been uploaded. This is useful to know, thank you.
    – Christine Cooper
    Commented Oct 3, 2014 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.