File Upload Large file catch

So I have the code below for file uploads. I got it here…

And modified it for my use. It uploads great but it’s not catching over sized files
with $CGI::POST_MAX = 1024 * 50; I only allow 50kb files.
Any ideas are appreciated.

sub upload1 {
my ($db_id) = @_;
my ($result,$ext,$buffer);
use CGI;
my $query = new CGI;
my $fileName = $query->param('FILE1'); 
$CGI::POST_MAX = 1024 * 50;

if ( !$fileName ){ $result = "NO UPLOAD1";  } ###########NO UPLOAD1
else{    
    if ($fileName=~ /.*.gif/) {$ext = ".gif"; }
    if ($fileName=~ /.*.GIF/) {$ext = ".gif"; }
    if ($fileName=~ /.*.jpg/) {$ext = ".jpg"; }
    if ($fileName=~ /.*.JPG/) {$ext = ".jpg"; }
    
        if($ext eq ""){ $result ="NO UPLOAD2"; }###########NO UPLOAD2
        else
        { 
        $fileName = $db_id; 
        $fileName = "$fileName" . "$ext";
        my $upload_dir = "$user_path/graphics/$db_id";
        my $upload_filehandle = $query->upload("FILE1");
        open ( UPLOADFILE, ">$upload_dir/$fileName" ) or die "$!";
        binmode UPLOADFILE;
         while ( <$upload_filehandle> ) {
        print UPLOADFILE;
        }
        close UPLOADFILE;
        $result ="$fileName";
        }#else ###########NO UPLOAD2
}########### else NO UPLOAD1
return $result;
}

ok… so… it appears that as i’m using cgi.pm standard that this feature won’t work. so i’m using this…

while (my $bytesread = read($file, my $buffer, 1024)) 
        { 
                $mytotalbytesread = ($bytesread + $mytotalbytesread);
                if($mytotalbytesread > "50000")
                { 
                $nouploadmess="NOUPLOAD";
                #&error("Your photo $fileName is too large ($mytotalbytesread bytes). Please reduce it to 300 x 225 pixels 
                #(50kb or 50000 bytes)"); exit;
            
                }
                else
                { 
                print OUTFILE $buffer;  
                }

Hi!

I’m not sure if you’re still working on this, but I’d like to let you know that using CGI.pm is deprecated . Personally I would use PSGI/Plack, and depending on the size of your project you might want to look at a PSGI-based framework like Dancer2 or the lightweight Web::Simple.

All the best.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.