10

I have been having lot of problems with users uploading images on my website.

They can upload up to 6 images

Originally I had to change values in php.ini to:

upload_max_filesize = 2000M
post_max_size = 2000M
max_execution_time = 120
max_file_uploads = 7
memory_limit=128M

I had to change to this as was getting all sorts of errors like out of memory, maximum post exceeded etc.

Everything was going ok till I checked my error log which contained :

[11-Jun-2011 04:33:06] PHP Warning:  Unknown: POST Content-Length of 113 bytes exceeds the limit of -1988100096 bytes in Unknown on line 0
[11-Jun-2011 04:33:12] PHP Warning:  Unknown: POST Content-Length of 75 bytes exceeds the limit of -1988100096 bytes in Unknown on line 0
[11-Jun-2011 04:33:27] PHP Warning:  Unknown: POST Content-Length of 74 bytes exceeds the limit of -1988100096 bytes in Unknown on line 0
[11-Jun-2011 04:33:34] PHP Warning:  Unknown: POST Content-Length of 75 bytes exceeds the limit of -1988100096 bytes in Unknown on line 0
[11-Jun-2011 04:33:43] PHP Warning:  Unknown: POST Content-Length of 77 bytes exceeds the limit of -1988100096 bytes in Unknown on line 0
[11-Jun-2011 04:33:48] PHP Warning:  Unknown: POST Content-Length of 74 bytes exceeds the limit of -1988100096 bytes in Unknown on line 0
[11-Jun-2011 04:33:53] PHP Warning:  Unknown: POST Content-Length of 75 bytes exceeds the limit of -1988100096 bytes in Unknown on line 0
[11-Jun-2011 04:34:20] PHP Warning:  Unknown: POST Content-Length of 133 bytes exceeds the limit of -1988100096 bytes in Unknown on line 0
[11-Jun-2011 04:35:29] PHP Warning:  Unknown: POST Content-Length of 131 bytes exceeds the limit of -1988100096 bytes in Unknown on line 0
[11-Jun-2011 04:36:00] PHP Warning:  Unknown: POST Content-Length of 113 bytes exceeds the limit of -1988100096 bytes in Unknown on line 0
[11-Jun-2011 04:36:06] PHP Warning:  Unknown: POST Content-Length of 75 bytes exceeds the limit of -1988100096 bytes in Unknown on line 0
[11-Jun-2011 04:36:34] PHP Warning:  Unknown: POST Content-Length of 116 bytes exceeds the limit of -1988100096 bytes in Unknown on line 0

if I change the post max value back top 8M I get message like this:

PHP Warning:  POST Content-Length of 11933650 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

Any ideas where I am going wrong?

hakre
  • 193,403
  • 52
  • 435
  • 836
daza166
  • 3,543
  • 10
  • 35
  • 41
  • 3
    Why would any sane person try uploading files as large as 2GB via a web form? – ThiefMaster Jun 11 '11 at 13:46
  • @ThiefMaster: Hmm, probably this has to do with the size of the files one wants to upload :). – hakre Jun 11 '11 at 13:50
  • There are better ways to upload files that large - FTP for example. – ThiefMaster Jun 11 '11 at 14:54
  • 2
    @ThiefMaster: Probably an existing web-based content system for media. I don't think there's anything wrong trying to get things to work with the least effort. What's the harm? And why is FTP "better"? And even if FTP is better, that doesn't make HTTP "wrong". – jastram Sep 23 '13 at 19:52

2 Answers2

22

On some 32bit systems PHP will take the memory settings like 2000M or 2G and convert it to the integer number of bytes by not performing a boundary check. A number starting at 2G or 2048M will be -2147483648 bytes then.

Some PHP versions cap this at the top, so it won't go into negative numbers (that is the 32 bit signed integer limit).

If you want to achieve the maximum possible number of bytes on such a system then, use 2147483647. This is equal to two gigabytes minus one byte.

Alternatively if you need to deal with large data, consider a 64bit system.

Additionally you should consider the following:

According to the PHP manual, the memory_limit setting is the more important one. If it does not offer enough memory, the post-data size-check then will pass, but PHP would not have enough memory to actually handle the post-data. You will get another error than, that the memory exceeded. So when you configure your PHP, take care that post_max_size is smaller than memory_limit.

In your example the memory_limit is 128M, so it can not process post-data of a size larger than ~128 Megabyte.

(This blog post shows what can happen and how large memory settings on 32bit and 64bit systems behave)

hakre
  • 193,403
  • 52
  • 435
  • 836
  • So what do you reckon I should change the memory-limit to and post-max-size should I increase memory or change post size? – daza166 Jun 11 '11 at 13:53
  • daza166: How large are the files you want to upload? How many files at once should be uploaded? – hakre Jun 11 '11 at 13:56
  • 6 images can be uploaded at once, size depends on photo could be just over 1MB most people upload. Photo that caused a problem the other day was a jpeg 982.6 KB (1006188 bytes) – daza166 Jun 11 '11 at 13:57
  • Okay, then calculate 2M per image, with six images, that's a setting of `12M` and images can a little bit larger then. That's compatible with your `memory_limit` setting of `128M` as well. – hakre Jun 11 '11 at 13:58
  • So would I need to change anything in this case is memory=128 and post-size & upload-size at 2000M going to be sufficient to stop this problem? 1000M for post and uplaod is not enough as 1006188 bytes is more. So should I leave everything them as 2000M and memory as 128M – daza166 Jun 11 '11 at 14:01
  • Set post-size and upload-size to `12M` -- do not use `2000M`!. I repeat this because it's important: Remove every `2000M` from the .ini file immediatly. Replace it with a value that makes sense. `12M` as far as we found out. If you're doing this on a public server, try to get in contact with a guy who knows a bit about configuration and let him review your computer and program code. – hakre Jun 11 '11 at 14:03
  • Ok Thanks have changed settings to upload_max_filesize=12M and post_max_size=12M and memory_limit=128M . Sorry need to ask one last thing, in my PHP script I have got $mb_limit=2000000 so dont allow image > $mb_limit. How much should I change this to now for 2M in bytes against each image? – daza166 Jun 11 '11 at 14:08
  • So what do I change value to in if($_FILES["uploadedfile_0"]["size"]<2000000) for 2M as we spoke as this is currently set to 2GB? – daza166 Jun 11 '11 at 14:16
  • `2M = 2K * 1024 = 2 * 1024 * 1024 = 2097152` – hakre Jun 11 '11 at 14:18
  • daza166: Can you say why you put 2G or 2000M in there? I'm just curious and would like to better understand. I first thought you wanted to allow really large file uploads but as it turns out that was not the case. – hakre Jun 11 '11 at 14:20
  • I was reading through some articles with this problem in my title thread and someone said change it to 2000M (2GB) so you wont get any errors with photo uploads size, so dont have to worry about size restrictions, but I see that advice was wrong causing more problems. Most photos are not more than 2MB really are they? – daza166 Jun 11 '11 at 14:21
  • What if I need to upload a really large file (say about 3GB)? – nik Sep 26 '12 at 07:08
  • @nik: Just do it. If you run into a problem, post your own question and outline what you did so far. Also document the error messages in your question. (Spreading comments here is not that nice (and productive)). You can also easily link questions to each other by placing a link :) – hakre Sep 26 '12 at 08:54
  • Now that is a strange bug. Thanks for the solution. – earl3s Jan 29 '13 at 22:33
7

It looks like your "2000M" is exceeding the integer limit. From the manual:

PHP allows shortcuts for bit values, including K (kilo), M (mega) and G (giga). PHP will do the conversions automatically if you use any of these. Be careful not to exceed the 32 bit signed integer limit (if you're using 32bit versions) as it will cause your script to fail.

try a smaller value, say 1000M. 2 Gigabytes of incoming data are probably unlikely anyway.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • What if I need to upload a really large file (say about 3GB)? – nik Sep 26 '12 at 07:04
  • @nik I'm not sure. There's related discussion here: http://stackoverflow.com/questions/864570/very-large-uploads-with-php but I'm not sure how many of the answerers have actually ever attempted *such* a huge upload – Pekka Sep 26 '12 at 17:58