11

Let's say I want to change the max upload limit for a Wordpress website and I give the following values:

wp-config.php: 128MB php.ini: 256MB .htaccess: 64MB

So, which one would Wordpress prioritize while processing upon need?

3 Answers 3

14

It's not clear from your question what you are changing in each of these files, but I presume in each case it is the upload_max_filesize PHP setting.

In general, settings will be applied in this order, each over-riding the previous value:

  1. php.ini
  2. Apache directives in .htaccess
  3. calls to ini_set()

However, this setting is defined as PHP_INI_PERDIR, which as explained on this page means it cannot be set using ini_set, so the wp-config.php cannot change it. So in the example you give in the question, it will take the value from .htaccess of 64MB.

You can verify this by running echo ini_get('upload_max_filesize'); somewhere in your code.

Note that there are other places values can be set which I haven't listed above, such as per-user php.ini files, and other Apache configuration contexts. Also, some of these can be disabled, so if your server is not set to allow over-rides in .htaccess, you won't be able to set a value there either.

Also note that Wordpress includes some of its own configuration variables, which interact in different ways with the PHP configuration. For instance, WP_MEMORY_LIMIT will attempt to raise the PHP memory_limit setting at startup, but has code which checks and never lowers it. There's no general rule to this, it will be different for different settings.

1
  • php.ini is always the first to choose.
    – Sabbir H
    Commented Feb 14, 2017 at 19:36
5

Basically all three files are taken into consideration.

WordPress/your server will check in the following order:

wp-config.php > .htaccess > php.ini

If something on a "higher level" (later in the chain) is limiting your value, the former value will be ignored or overwritten. If a piece of the chain is missing, the value in .htaccess for example, the next higher one will be used instead.

In your example the .htaccess would limit the max_upload_size to 64 MB, although your server would be fine with up to 256 MB and WordPress would also accept up to 128 MB in file size first.

You can also set a different limit via the .htaccess file and by doing this you overwrite the value from php.ini, so that it is no longer the dominating value. This works in a lot of hosting environments, so chances are quite good you can raise or lower the max_upload_size in this way.

If you have a more strictly configured hosting/server, it is possible that the option to override the settings from php.ini is disabled. In this case, overriding max_upload_size from .htaccess won´t work, so this could be a limiting factor.

7
  • 8
    How is this "not how it works" when you give a list of priorities, just as the question asks for? And if .htaccess is in the middle of the list, why would that one be the limiting factor?
    – IMSoP
    Commented Feb 13, 2017 at 14:37
  • I think my expression was unclear. I understood his question as "if I set a higher value in wp-config, will that 'dominate' the others?" and that is not how it works. ;-) @bns did a better explanation on that.
    – flomei
    Commented Feb 14, 2017 at 5:36
  • Right, I see what you mean now. However, as I commented under bns's answer, I can find no documentation backing up the claim that you can't override with a higher value. The wp-config.php value will always be ignored, as explained in my answer, but I'm pretty sure a higher value in .htaccess would override a lower one in php.ini just fine.
    – IMSoP
    Commented Feb 14, 2017 at 8:39
  • You are partially right, but overriding php.ini values from .htaccess can be deactivated, see my updated answer.
    – flomei
    Commented Feb 14, 2017 at 8:58
  • Your update still implies you can only set a lower value via .htaccess. I know of no reason you couldn't also set a higher value there. The fact that the mechanism can be disabled is a good point, and one I glossed over in my answer, but assuming any PHP settings are allowed in .htaccess, then a value in .htaccess will absolutely "dominate" a value in php.ini for the same setting.
    – IMSoP
    Commented Feb 14, 2017 at 9:33
3

I think flomei should edit the answer, because that is the right answer but bad explaination. Php.ini -> .htaccess -> wp-config.php is actually the order each file will be read and will set the values taking consideration that there is not setting previously defined. But in the case there is a previously defined setting, this will be "over-ridden" only if the new setting is lower.

That means, if you have wp-config.php with 64Mb and .htaccess with 32 MB: wp-config.php won't override that setting because there is already a lower limit and you will end up with 32Mb.

But if you have wp-config.php with 32Mb and .htaccess with 64 MB, wp-config.php will lower the previous setting to 32Mb.

Edit: To clarify, as IMSoP points out, wp-config.php just lets you set a size that's more restrictive than what the PHP settings allow if you are changing 'WP_MEMORY_LIMIT', it doesn't let you go beyond that. So it's not actually a settings override. In fact, there are separated checks done at different points at runtime. If you are changing the php setting upload_max_filesize as he assumes (the question doesn't indicate which settings are you changing), that has no effect.

Also, you will have to take into account that the post_max_size must be greater than upload_max_filesize in your php settings

8
  • 2
    Can you link to any documentation for this "only over-riding if the new value is lower", because it's certainly not true of any other PHP setting I've seen. Nor is it mentioned on this manual page. What is mentioned, as I point out in my answer, is that upload_max_filesize cannot be changed at runtime (presumably because it's too late for it to have any effect) so wp-config.php can't affect it one way or the other. A built-in default of 2MB is also mentioned, and you can definitely set it higher than that.
    – IMSoP
    Commented Feb 13, 2017 at 23:39
  • That's basically what my hosting provider support team expained to me when I had the same issue, I will search for the e-mail and update if I find it. I am not really sure if inside the server actually you override the value, or it's just that verifications are done in execution time at different places, thus the behavior is like this.
    – bns
    Commented Feb 14, 2017 at 14:59
  • 1
    Just to reiterate, if the line you're adding to wp-config.php is ini_set('upload_max_filesize', '32M'); it will have no effect, ever, because that setting cannot be set at run-time as it is defined as PHP_INI_PERDIR. (Unless that documentation page is wrong, but that seems unlikely.)
    – IMSoP
    Commented Feb 14, 2017 at 15:06
  • No, you are right. It's not really a settings override. Its just another separated restriction, i will update acordingly to avoid confusion.
    – bns
    Commented Feb 14, 2017 at 15:54
  • 1
    @IMSoP / bns - I think there has perhaps been a bit of confusion between specific wp-config.php values and setting arbitrary PHP values with ini_set(). As I understand it, wp-config.php is primarily to do with setting WordPress specific config values and many of these are indeed restricted (ie. imposed upper limit) by the underlying PHP config setting (such as WP_MEMORY_LIMIT as mentioned in the answer). This is a WordPress thing. However, when setting arbitrary PHP config values using ini_set() in PHP there is no such restriction.
    – MrWhite
    Commented Feb 15, 2017 at 23:03

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.