88

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
  • 43,016
  • 36
  • 114
  • 168
  • 9
    "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

73

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
  • 76,400
  • 86
  • 326
  • 602
Rob
  • 46,632
  • 4
  • 70
  • 91
  • 13
    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
  • 2
    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
72

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
  • 326,960
  • 63
  • 404
  • 402
  • 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
  • 5
    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 Jackson 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. – H. Farid Feb 01 '19 at 13:03
43

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
  • 974
  • 8
  • 9
12

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
  • 12,786
  • 3
  • 34
  • 71
dwenaus
  • 2,781
  • 2
  • 22
  • 27
6

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
  • 49,611
  • 27
  • 114
  • 164
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,626
  • 3
  • 22
  • 35
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
  • 370
  • 3
  • 9
0

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,083
  • 2
  • 16
  • 23
0

By default, PHP permits uploading maximum 2 MB file on the server. But you can modify the maximum size of file upload as per your condition. By using the PHP configuration file php.ini, you can increase or decrease the file upload size in PHP.

First open the php.ini file in your text editor. Search the upload_max_filesize variable and stipulate the size which you want to increase.

Search for post_max_size variable and stipulate the size which you want to increase.

post_max_size = 128M

you can check this from here

hammad khan
  • 252
  • 1
  • 5
-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.

sassman
  • 645
  • 5
  • 8
-11

You can use also in the php file like this

<?php ini_set('upload_max_filesize', '200M'); ?>
Flexo
  • 82,006
  • 22
  • 174
  • 256
Nishant Patel
  • 321
  • 1
  • 5
  • 22