Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

File Upload Size Check

by coolsaurabh (Acolyte)
on Aug 05, 2019 at 12:00 UTC ( [id://11103925]=perlquestion: print w/replies, xml ) Need Help??

coolsaurabh has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I am facing strange issue in cgi script. Requirement is to upload the file and to print log message in case file size is more than 5 MB. Script is throwing error in case file size is bigger than 5 MB. However it is working fine case if the file size is less than 5 MB. Any suggestions.Pls advice.

#!/usr/bin/perl use CGI qw(:standard); use CGI::Carp qw( fatalsToBrowser ); use File::Basename; use Net::OpenSSH; use Net::SSH::Expect; use Data::Dumper; use Exporter qw(import); our @EXPORT = qw(copyToTarget); sub print_page() { print ("Content-type: text/html\n\n"); print <<__HTML__; <form name=f1 style="margin:20px 0" action="Maintenance_Framew +ork.cgi" method="post" enctype="multipart/form-data"> <p> <h3 align='center'> Maintenance File Upload </h3> <br> <p style="margin-left:16.5em;font-size:20px">Customer:<select +name="Customer" > <option value="Telenor_PK">Telenor_PK</option> <option value="Vodacom_TZ">Vodacom_TZ</option> </select> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n +bsp;&nbsp;&nbsp;&nbsp; Technology : <select name="Techno" > <option value="Core">Core</option> <option value="RAN">RAN</option> </select> </p> <br> <p>File to Upload: <input type="file" name="filecsv" /></p> <p><input type="submit" name="Submit" value="Upload"></p>&nbsp +&nbsp&nbsp </body> </html> </form> __HTML__ } sub main() { $CGI::POST_MAX = 1024 *1024 ; my $safe_filename_characters = "a-zA-Z0-9_.-"; my $upload_dir = "/opt/IBM/Maintenance/tmp"; my $query = new CGI; print_page(); my $filename = $query->param("filecsv"); my $ctext = $query->param("Customer"); my $ttechno = $query->param("Techno"); my $content_length = $ENV{'CONTENT_LENGTH'}; if ( !$filename ) { print<<END_HTML2; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//E +N" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="e +n" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; ch +arset=utf-8" /> <title>Thanks!</title> <style type="text/css"> img {border: none;} </style> </head> <body> <p>File is too big for upload! $content_length</p> </body> </html> END_HTML2 exit; } else { print<<END_HTML3; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//E +N" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="e +n" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; ch +arset=utf-8" /> <title>Thanks!</title> <style type="text/css"> img {border: none;} </style> </head> <body> <p>File is in relevant size to upload! $content_length +</p> </body> </html> END_HTML3 } }

Replies are listed 'Best First'.
Re: File Upload Size Check
by marto (Cardinal) on Aug 05, 2019 at 12:11 UTC
      Yes it should be what you suggested. However when i am trying to upload the file greater than 5MB size,it is throwing some error.

        Your code posted has a limit of 1MB. If you want a 5MB limit set it to what I showed you, if you want to upload something bigger, change the multiplier. Also ensure your web server isn't configured to limit file uploads.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11103925]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-19 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found