views:

73

answers:

3

I am having a strange problem while uploading large files in PHP.

In php.ini, max_execution_time is set to 30, post_max_size is set to 32M, upload_max_filesize is set to 32M. When I tried to upload a file of size 40.2 MB, it don't show any error. The $_FILES variable has the value array(0) { } and $_FILES['userfile'] shows NULL.

If the file size is more than the value set in php.ini, then it should return the error message

UPLOAD_ERR_INI_SIZE, Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

But it's not showing any error either (as $_FILES is an empty array). I am clueless why this is happening.

When I change in php.ini and set post_max_size is set to 64M, upload_max_filesize is set to 64M, then it works fine. So, I decided to use the following code, instead of changing php.ini file.

ini_set('upload_max_filesize', '64M');
ini_set('post_max_size', '64M');
ini_set('max_execution_time', 300);

I even tried to increase max_execution_time. Still, I am having the same problem. ini_set() is not working here.

+5  A: 

To have the 40 MB file fail with upload error, you have to increase the post_max_size, if you exceed the value of that variable, you get an empty $_FILES array. See the manual

If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty.

Also, ini_set() is not working there because two of the variables you are trying to change are PHP_INI_PERDIR and thus need to be changed in php.ini or in .htaccess or httpd.conf. You should try the 40MB file with, for example, these settings in .htaccess

php_value upload_max_filesize 32M
php_value post_max_size 64M
php_value max_execution_time 300
Vinko Vrsalovic
Best answer, except you cannot set upload_max_filesize and post_max_size using ini_set(), only via .conf or .ini files.
Michael Butler
@Michael: Yes, realized that later, already fixed, thanks
Vinko Vrsalovic
A: 

There is one more setting you may need to look at, Apache's LimitRequestBody.

If the file exceeds that, the upload may get blocked before it even reaches PHP.

Apache Documentation

Pekka
This is also a good point. I never knew it. Thanks for sharing.
Debiprasad
+1  A: 

ini_set() is not working here.

The values you are trying to change with ini_set(), except for max_execution_time, cannot be changed with ini_set().
In the list of php.ini directives, they are reported to be of type PHP_INI_PERDIR, which means (as explained in Where a configuration setting may be set) that they can changed in php.ini, .htaccess or httpd.conf. Configuration settings that can be changed with ini_set() are the ones marked as PHP_INI_USER.

kiamlaluno
I want to mark this as accepted answer, but Stack Overflow allows to mark only one answer as accepted answer, which I already marked.
Debiprasad
@Debiprasad: It's not possible to have two accepted answers; you should deselect the previously selected, before to select another answer as accepted.
kiamlaluno