Page 1 of 1

Display an upload error attemting uploading a 2 large file?

Posted: Wed Jan 31, 2007 7:03 am
by josefv
I have a php script that uploads an image to my mySQL DB. My MAX_FILE_SIZE setting has been set to 1000000. Now, how do I get my php page to display a nice message to my visitor when he has exceeded the allowable file size.

I know its not possible with JavaScript, due to security reasons, and I'm also not prepared to use ActiveX.

Any ideas?

Posted: Wed Jan 31, 2007 7:12 am
by dude81

Posted: Wed Jan 31, 2007 8:15 am
by feyd
$_FILES['yourFileFieldName']['error'] will contain information, also look at $_FILES['yourFileFieldName']['size'].

Posted: Wed Jan 31, 2007 10:08 am
by dhrosti

Code: Select all

$max_size = $_POST['MAX_FILE_SIZE'];
$upload_size = $_FILES['yourFile']['size'];
if ($upload_size > $max_size) {
  $error = "1";
  $errormsg = "Uploaded file is too big!";
}
Then tell php to stop uploading if $error = 1 and display $errormsg somewhere within your page.

btw, iv just got into php aswell and it probably has its faults but this is just how i went about doing a similar thing

Posted: Wed Jan 31, 2007 11:19 am
by feyd
Trusting the posted max_file_size allows someone to specify any size they wish.

Posted: Thu Feb 01, 2007 1:17 am
by josefv
Im using action="$_SERVER['PHP_SELF']" on my form. When the form submits, and I check the error and size values of my files, they are empty. I assume that the file was not uploaded due to the MAX_FILE_SIZE hidden field setting, and therefore there appears to be nothing in $_FILES to check errors agains. I keep on getting empty errors even though I am submitting too large files.

Flabbergasted i am!?!?

Posted: Thu Feb 01, 2007 1:22 am
by superdezign
Well you're server probably stops the upload before your php script gets to it, I'm assuming. Check phpinfo() for post_max_size and max_file_size (?) and use $_FILES['yourFile']['error'] to determine if an error has occurred

Posted: Thu Feb 01, 2007 1:52 am
by feyd
Is $_FILES being filled at all? If not, verify that your form is set to the proper enctype attribute.

Posted: Fri Feb 02, 2007 2:06 am
by josefv
The script works perfectly when the image is within the proper file size constraint. However, the moment the file is larger than my MAX_FILE_SIZE setting, the $_FILES array does not get filled. So I can't manage to catch the error to display a user-friendly message to my not so very technically talented site visitor.

My page simply pops back, and is reset.