tags:

views:

827

answers:

6

I have a PHP form for uploading files and it works fine and displays an error message if something went wrong. This is all good.

The problem is when I test with a really big file, it just refreshes the page as if I hadn't sent a file at all, and none of the $_POST variables from the form are even sent to the server.

I want to display an error to the user, letting them know that the file is too big. However, I can't do this.

Does anyone know what is happening?

+5  A: 

Check your PHP ini file, which governs how large a file PHP will allow to be uploaded. These variables are important:

  • max upload filesize (upload_max_filesize)
  • max post data size (post_max_size)
  • memory limit (memory_limit)

Any uploads outside these bounds will be ignored or errored-out, depending on your settings.

This section in the docs has the best summary: http://ca3.php.net/manual/en/features.file-upload.common-pitfalls.php

EDIT: Also note that most browsers won't send uploads over 2GB in size. This link is outdated, but gives an idea: http://www.motobit.com/help/scptutl/pa98.htm. Anyone have a better source of info on this?

There are also limits that can be imposed by the server, such as Apache: http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestbody

To truly see what's going on, you should probably check your webserver logs, check the browser upload limit (if you're using firefox), and try seeing if print_r($_FILES) generates any useful error numbers. If all else fails, try the net traffic monitor in firebug. The key is to determine if the request is even going to the server, and if it is what the request (including headers) looks like. Once you've gotten that far in the chain, then you can go back and see how PHP is handling the upload.

Jarret Hardie
I'm exceeding the limit on purpose to check my error handling is working. The lack of useful errors is the problem. The browser refreshes, there are no errors and no files or post variables are sent.
Richard
Ah, I see. Apologies. How big is the file? I've seen this with other languages, and it's usually a browser limit, or your webserver protecting you. I've found that anything over 2GB tends to be problematic.
Jarret Hardie
This link is outdated, but gives the general gist: http://www.motobit.com/help/scptutl/pa98.htm
Jarret Hardie
Only 16MB. Over the 8MB limit I've set, but not GB huge.
Richard
It doesn't work on my local test server still, but it now works fine on the live server. Thanks for your help.
Richard
A: 

I would implement a simple script that does regular ajax calls to know how much of the upload has been done. You can then implement some sort of progress bar. There are a couple of examples on the net:

http://martinjansen.com/2007/04/28/file-upload-progress-bars-with-php/

m_oLogin
+2  A: 

Your $_POST is most likely empty because the upload exceeded the *post_max_size* directive:

From PHP's directives page:

If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty. This can be tracked in various ways, e.g. by passing the $_GET variable to the script processing the data, i.e. <form action="edit.php?processed=1">, and then checking if $_GET['processed'] is set.

altermativ
Thanks, that's really good to know and explains a lot.
Richard
A: 

you can not know the size of the file that is being uploaded, until it gets fully uploaded. so if you want to determine the file size, and then show an error for large files, first you must have the file uploaded, then check the file size. inorder to have a large file uploaded, you should set 2 settings in your php configuration file, php.ini. open it up and search for "upload_max_filesize" and set the value to maximum value you want. then you must set the maximum value for POST parameters, because uploaded files are recieved via HTTP POST method. find "post_max_size" and set it to a large value to.

farzad
A: 

max upload filesize (upload_max_filesize) max post data size (post_max_size) are the directives you need to set. I have tested it with multiple OS, browsers etc and these are the only two thing you need be worried about.

A: 

Don't know if my answer will be posted but will try anyways. Yes, the answer is what altermativ posted - post_max_size in php.ini. Just set it to the largest value sensibly possible and you'll at least start getting errors from $_FILES superglobal, given of course you have that sort of error validation showing on your screen scripted. There is an error for post_max_size in $_FILES superglobal when too large files are uploaded, but it only gets triggered if same setting's value in php.ini has not been exceeded. If it is exceeded during file upload, you can only find the error in the webserver's error log. Hope I was clear in my clarification. Regards to all...

Igor K.