Recently was setting up a form that allows the user to attach a file and ran into some odd behavior (or so it seemed at first). After noodling around I realized what the problem was. I searched around here and didn't find a similar situation, so I thought I'd post my "solution" and see if anyone had a better idea.
so I set up a form to accept file attachments no larger than 2MB. No problem, as the server allowed 4MB uploads. Had it set to check that the file wasn't larger than 2MB, returning a error message if it was:
Everything worked just peachy-keen until I uploaded a file larger than 4MB. At that point, there was no error message, and the form got processed "normally", but no attachment was sent.
@IF@attachedFile@attachedFile#<2097152@@Maximum file upload size is 2MB.@
So obviously (now, but not at the time), the server was intercepting the uploaded files, seeing that it was larger than 4MB, and dumping it. This meant that as far as the script was concerned, nothing was being attached. No attachment, no need to check the size, move along: nothing more to see here.
This has obvious problems, because the user may think their file made it. Nothing was saying otherwise.
Obviously we can't get past the server file size limitation because the server is First in All Things. So how to handle this unpleasantness (server-side)? My solution:
I added a checkbox for the user to tick, indicating they were attaching a file:
<input type="checkbox" name="fileAttached" value="SeeAttached" id="fileAttached" />
that last sentence elimitates some silliness: if the user attached a file but it was too honkin' big, then that's a bit of clarification. ("Waddaya mean 'Attach the file'? I DID attach the file!") I may increase the attachment limit to 4MB just to make that tidier. Anyway, pretty clear what I'm doing there. "This box is checked but there ain't nothin' in the file field, so what up, d00d?"
@IF@fileAttached=/SeeAttached/@@attachedFile@It appears you
have indicated that you are including additional documentation. Please attach the file
and resubmit the form. If you attached a file, please note that you are not
permitted to attach files larger than 2MB, and the file attached has exceeded 4MB.
Ok, great, but this is still only halfway there. Obviously we don't want that checkbox to be required because not everyone is uploading something. But what if they upload without checking the box? So...:
the problem with this, of course, is we're right back at the beginning: server intercepted file; server nuked file; form field is blank; user doesn't know file got stripped. (ok, it's silly, too.)
@IF@attachedFile=/!^$@@fileAttached@It appears you have attached a file.
Please go back and check the box indicating that a file is attached.@
If anyone has a better (less-kludgy or non-kludgy) solution around this awkwardness, I'd luuuuurve to hear about it!
btw, unless I'm missing something (and I only hack at php, so yeah, prolly am) this bit:
appears to be checking if the attached file is SMALLER than 2MB. < is less-than, > is greater-than. so shouldn't it be