Forum Moderators: coopster

Message Too Old, No Replies

Max file size for upload

How to show a friendly error?

         

barns101

5:57 pm on Aug 15, 2006 (gmt 0)

10+ Year Member



Looking at the PHP documentation, they recommend adding the following code to forms when uploading files:


<input type="hidden" name="MAX_FILE_SIZE" value="30000" />

When a larger file is uploaded a warning is issued. I can't seem to find any way to work with the MAX_FILE_SIZE to a) suppress the warning, and b) display a friendly error message to the visitor.

It's probably something really simple that I've missed... ;)

dreamcatcher

6:49 pm on Aug 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Personally, I never use that code, I just work with the size thats in the $_FILES array.

$max = '30000';

<input type="file" name="file">

$size = $_FILES['file']['size'];

if ($size > $max)
{
// error
}

dc

barns101

7:00 pm on Aug 15, 2006 (gmt 0)

10+ Year Member



Thanks, dreamcatcher. Using the $_FILES array was my first instinct and also the only solution that I found on the web. It's reassuring to know that you're doing the same. :)

So, is there a practical use for the MAX_FILE_SIZE field other than generating an error message? It seems pretty useless from what I can find. :o

dreamcatcher

7:26 pm on Aug 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If a browser supports this field, it will not allow the user to choose a file that is larger than the specified bytes. So, the user does not have to wait for a file to be uploaded in order to find out whether it is too large or not.

I dunno, I guess some people have a use for it. The MAX_FILE_SIZE field can be changed client side, so its not going to be a solid check for a file size anyway. If you want to check if a file is too big for your server to process use the upload_max_filesize directive.

dc

barns101

7:46 pm on Aug 15, 2006 (gmt 0)

10+ Year Member



That's odd. I tried my upload form in both Firefox 1.5.06 and IE 6 and both browsers appeared to have uploaded larger files than they were supposed to (because PHP gave me the warning message).