tags:

views:

191

answers:

3

I was trying to make a file upload form and checked the PHP documentation to refresh my memory on the subject. Here is a link to the relevant article. All of a sudden I noticed this message:

The MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted by PHP. This form element should always be used as it saves users the trouble of waiting for a big file being transferred only to find that it was too large and the transfer failed. Keep in mind: fooling this setting on the browser side is quite easy, so never rely on files with a greater size being blocked by this feature. It is merely a convenience feature for users on the client side of the application. The PHP settings (on the server side) for maximum-size, however, cannot be fooled.

OK... Say what? First it tells that it must precede the file upload field. Then it tells us that it is merely for convenience. And besides - it's on client side anyway so anyone can mess with it. After googling around I also found information that there are no known browsers that support it.

WTF? Why is it said that it must precede the file upload field if it seems to be (for all intents and purposes) absolutely pointless? Should I bother putting it in my HTML at all?

+2  A: 

What follows is me being wrong, please read the other answers which are better-informed, and accurate (AFAIK).

I think the point is exactly as it states:

This form element should always be used as it saves users the trouble of waiting for a big file being transferred only to find that it was too large and the transfer failed

Yes, it can be fooled, and so shouldn't be relied on to prevent larger files from being uploaded, but for non-malicious users if the uploaded file is bigger than the integer in this field, PHP disallows this upload and presents an error code in the $_FILES array (source - comments on php.net).

Dominic Rodger
How will this warn users if the browsers ignore it?
Vilx-
@Vilx - I've hopefully clarified my answer. I think PHP somehow checks for the existence of the `max_file_size` field before processing the post data, but I'm not sure quite how that works.
Dominic Rodger
Funny then, because it sure doesn't do it on my WinXP + Apache 2.2 + PHP 5. And google reveals that I'm not alone in this.
Vilx-
Users who've +1ed me, it appears I'm wrong, could you un+1 me. Then references in comments and other answers to the question will make sense, but no one will listen to what I wrote!
Dominic Rodger
+6  A: 

At the moment there are no browsers that actually care about the MAX_FILE_SIZE directive so it is pretty pointless. I suppose it does give you more granular control over max sizes on upload (as the poster above stated) rather than going with php.ini's, but personally I just ignore it, and you probably should too. It will certainly not stop a user uploading a larger than required file - the manual is fairly misleading in this regard.

Meep3D
+1  A: 

I believe the point is that conformant browsers would prevent form submission in the case where the user selected a file that was too large, which would save them having to perform at least a partial upload (which could take a while) of a file that was going to be rejected.

On the server side, PHP still checks and enforces the various limits set in PHP.ini, and will reference the fact that an upload was too large in the normal manner, i.e. an error code set in $_FILES. You might think of the field as an analogy to JavaScript validation - we might do a quick client-side check for the user's convenience, but we still do a proper server-side test and enforce it for all requests.

As others have stated, there don't appear to be any browsers that actually bother to perform this check, making it relatively useless.

Rob
Actually, it's not even in any standards besides PHP's own documentation, so no wonder there are no browsers or even plugins that implement this. Check here: http://bugs.php.net/bug.php?id=40387
Vilx-
Indeed. I use the term "conformant" scoped against that documentation.
Rob
OK then. :D
Vilx-