Display an upload error attemting uploading a 2 large file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
josefv
Forum Newbie
Posts: 23
Joined: Wed Jan 17, 2007 11:17 am

Display an upload error attemting uploading a 2 large file?

Post 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?
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_FILES['yourFileFieldName']['error'] will contain information, also look at $_FILES['yourFileFieldName']['size'].
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Trusting the posted max_file_size allows someone to specify any size they wish.
josefv
Forum Newbie
Posts: 23
Joined: Wed Jan 17, 2007 11:17 am

Post 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!?!?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is $_FILES being filled at all? If not, verify that your form is set to the proper enctype attribute.
josefv
Forum Newbie
Posts: 23
Joined: Wed Jan 17, 2007 11:17 am

Post 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.
Post Reply