How do I produce a useful error message when a file upload is toobig?

S

slugger3113

I have a CGI script that allows users to upload image files to a
website. I limit the file size to 75K.

The problem is if someone tries to upload a file larger than 75K, and
the user clicks Submit, the script basically just resets itself with
no warning; none of the functions get triggered.

Can someone tell me how to capture that "event" when a file size is
larger than 75K, and allow me to display a proper error message ("File
too big...")?

Here's what I've got, which successfully uploads the file if it's 75K
or lower, but doesn't display an error message if it's bigger:

my($imageFileMaxSize) = 75000;
$CGI::pOST_MAX = $imageFileMaxSize;

# other stuff defining image name, path, etc.

sub create_new_image_file {
my $newImageFile = @_;

$file = param('imagefile');

open (SAVE, ">$newImageFilenamePath") || on_error("Can't save $file
to $newImageFilenamePath");
while (read($file,$data,1024)) {
print SAVE $data;
$imageFileLength += length($data);
on_error("Image file is too large; maximum size allowed is
$imageFileMaxSize")
if($imageFileLength > $imageFileMaxSize);
}
close(SAVE);
on_error("File was not received; check filename.")
if($imageFileLength < 1);

}

thanks for any advice!
Scott
 
S

Sherm Pendley

slugger3113 said:
I have a CGI script that allows users to upload image files to a
website. I limit the file size to 75K.

The problem is if someone tries to upload a file larger than 75K, and
the user clicks Submit, the script basically just resets itself with
no warning; none of the functions get triggered.

Can someone tell me how to capture that "event" when a file size is
larger than 75K, and allow me to display a proper error message ("File
too big...")?

Here's what I've got, which successfully uploads the file if it's 75K
or lower, but doesn't display an error message if it's bigger:

my($imageFileMaxSize) = 75000;
$CGI::pOST_MAX = $imageFileMaxSize;

# other stuff defining image name, path, etc.

sub create_new_image_file {
my $newImageFile = @_;

$file = param('imagefile');

According to the CGI.pm docs, param() returns an empty list if the
post is too large, and cgi_error() returns "413 POST too large".

if (!$file && cgi_error()) {
# Handle the error
}
open (SAVE, ">$newImageFilenamePath") || on_error("Can't save $file
to $newImageFilenamePath");
while (read($file,$data,1024)) {
print SAVE $data;
$imageFileLength += length($data);
on_error("Image file is too large; maximum size allowed is
$imageFileMaxSize")
if($imageFileLength > $imageFileMaxSize);

This only triggers if more than POST_MAX bytes have been read. But, if
the uploaded file is too large, then *nothing* will be read, so this
check never gets triggered.

sherm--
 
S

slugger3113

That was it, thank you!

Scott

According to the CGI.pm docs, param() returns an empty list if the
post is too large, and cgi_error() returns "413 POST too large".

    if (!$file && cgi_error()) {
        # Handle the error
    }


This only triggers if more than POST_MAX bytes have been read. But, if
the uploaded file is too large, then *nothing* will be read, so this
check never gets triggered.

sherm--

--
My blog:http://shermspace.blogspot.com
Cocoa programming in Perl:http://camelbones.sourceforge.net- Hide quoted text -

- Show quoted text -
 
T

Tad J McClellan

slugger3113 said:
Here's what I've got, which successfully uploads the file


If you say so.

sub create_new_image_file {
my $newImageFile = @_;


You never use this variable, so what is it there for?

If you did mean to use it, you realize that it contains a number, don't you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top