100

I'm using PHP 5.3.0 and have encountered something that might be a bug (in which case I'll report it) or might be me - so I'm asking to make sure.

When running this code:

<?php
ini_set('upload_max_filesize', '10M');
echo ini_get('upload_max_filesize'), ", " , ini_get('post_max_size')

I end up with:

2M, 8M

This is despite my php.ini setting these higher:

upload_max_filesize = 10M
post_max_size = 10M

(occuring only once)

Because the error occurs after setting the value as well as it being set in php.ini I'm inclined to think it's a bug. Can anyone confirm or point me where I'm going wrong?

Update: Looks like restarting Apache fixed this - I always thought it didn't need to be restarted if you changed php.ini.

Ross
  • 46,186
  • 39
  • 120
  • 173
  • 11
    "I always thought it didn't need to be restarted if you changed php.ini." PHP CLI picks up changes immediately, because it parses php.ini with every invocation. mod_php parses php.ini once -- when apache starts up. – Frank Farmer Jul 14 '09 at 02:32
  • I had the same problem recently. upload_max_filesize wouldn't get into effect without restarting Apache. I'm on a PHP 5.2.9. After the restart everything is working okay. – Haluk Feb 25 '10 at 08:37
  • To avoid a full apache restart, just use "sudo service apache2 reload" – user1048839 Jun 04 '15 at 20:25

11 Answers11

82

You can't use shorthand notation to set configuration values outside of PHP.ini. I assume it's falling back to 2MB as the compiled default when confronted with a bad value.

On the other hand, I don't think upload_max_filesize could be set using ini_set(). The "official" list states that it is PHP_INI_PERDIR .

Pacerier
  • 86,231
  • 106
  • 366
  • 634
Rob
  • 47,999
  • 5
  • 74
  • 91
  • 20
    You think right! You can't set upload_max_filesize using ini_set() because upload_max_filesize is a PHP_INI_PERDIR type that means changeable only via: php.ini, .htaccess or httpd.conf as stated at: http://php.net/manual/en/configuration.changes.modes.php – Marco Demaio Feb 12 '10 at 12:03
  • 3
    Actually, you can use shorthand notation outside of PHP.ini; you can use it in `.htaccess` and also with `ini_set`. Maybe not in all versions, though. – Protector one Apr 13 '11 at 21:08
79

Are you using a shared hosting provider? It could be master settings overriding anything you're trying to change. Have you tried adding those into your .htaccess?

php_value upload_max_filesize 10M
php_value post_max_size 10M
karim79
  • 339,989
  • 67
  • 413
  • 406
  • 1
    No, this is my own Apache/PHP instance on my machine (which is Windows if it's relevant). I'll try adding those to the Apache config. – Ross Jul 13 '09 at 22:28
  • 6
    Update: This does affect it (changes them to 10) so this method works. I'm still quite confused as to why it's not working in php.ini or using ini_set. – Ross Jul 13 '09 at 22:30
  • +1 this is definitely the way to go if you can't get to php.ini - thank you very much. – Alex Coplan Dec 29 '11 at 13:16
  • 3
    Note this only works with Apache running PHP as Module, not as CGI – ChrisV Jun 18 '12 at 09:57
  • I would say this is the preferred method even with access to the `php.ini` file. Allows you to set these permissions based on need rather than as a global setting. – Bryant Makes Programs Dec 20 '17 at 19:27
  • This not works for me, and I can't override the master configuration of my free hosting service, at least for my domain. – HF_ Feb 01 '19 at 13:03
48

Since I just ran in to this problem on a shared host and was unable to add the values to my .htaccess file I thought I'd share my solution.

I made an ini file with the values in it. Simple as that:

Make a file called ".user.ini" and add your values

upload_max_filesize = 150M
post_max_size = 150M

Boom, problem solved.

quid
  • 1,024
  • 9
  • 9
14

I got this to work using a .user.ini file in the same directory as my index.php script that loads my app. Here are the contents:

upload_max_filesize = "20M"
post_max_size = "25M"

This is the recommended solution for Heroku.

Darren
  • 13,050
  • 4
  • 41
  • 79
dwenaus
  • 3,206
  • 2
  • 27
  • 27
7

This can also be controlled with the apache configuration. Check the httpd.conf and/or .htaccess for something like the following:

php_value upload_max_filesize 10M
Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
1

Please be advised that setting a large post_max_size or upload_max_filesize for a complete server or a complete virtual host is not a good idea as it may lead to increased security risks.

The risk is that an attacker may send very large POST requests and overloading your server memory and CPU as it has to parse and process those requests before handling them to your PHP script.

So it's best to limit changing this setting to some files or directories. For example if I want to /admin/files/ and /admin/images/ I can use:

<If "%{REQUEST_URI} =~ m!^/admin/(files|images)/! && -n %{HTTP_COOKIE}">
    php_value post_max_size 256M
    php_value upload_max_filesize 256M
</If>

I also require the request to have a cookie to avoid basic attacks. This will not protect you against attacks coming from non-authenticated users, but may delay any attack.

This setting can be used in Apache server configuration files, and .htaccess files as well.

bohwaz
  • 27
  • 5
0

If you are running in a local server, such as wamp or xampp, make sure it's using the php.ini you think it is. These servers usually default to a php.ini that's not in your html docs folder.

Beachhouse
  • 4,972
  • 3
  • 25
  • 39
0

I've faced the same problem , but I found out that not all the configuration settings could be set using ini_set() function , check this Where a configuration setting may be set

Hassan Nemir
  • 472
  • 4
  • 9
-1

if you use ini_set on the fly then you will find here http://php.net/manual/en/ini.core.php the information that e.g. upload_max_filesize and post_max_size is not changeable on the fly (PHP_INI_PERDIR).

Only a php.ini, .htaccess or vhost config change seems to change these variables.

5422m4n
  • 790
  • 5
  • 12
-2

This solution can be applied only if the issue is on a WordPress installation!

If you don't have FTP access or too lazy to edit files,

You can use Increase Maximum Upload File Size plugin to increase the maximum upload file size.

Disapamok
  • 1,426
  • 2
  • 21
  • 27
-13

You can use also in the php file like this

<?php ini_set('upload_max_filesize', '200M'); ?>
Flexo
  • 87,323
  • 22
  • 191
  • 272
Nishant Patel
  • 335
  • 1
  • 5
  • 23