Solved

saving an uploaded file

Posted on 2000-04-09
2
207 Views
Last Modified: 2010-03-05
Hello,
I have an HTML form which has a field in which the user can specify a file to be uploaded to the HTTP server.
I am now writing the perl script which will save it locally (on the HTTP server machine).  I am looking for sample code which shows how to do this save.  Specifically, I'm wondering how to get the file handle for the uploaded file or do I even need the file handle to do the save?
0
Comment
Question by:chichioco
2 Comments
 
LVL 3

Expert Comment

by:jyu_88
Comment Utility
$file = $query->param('fileComeIn');
# fileComeIn will be upload field name in the form
while( $bytesread = read($file, $dataread, 1024) )
{
    print TEMPHANDLE $dataread
}
close TEMPHANDLE;
you may want to check for size of the uploaded file by sum up bytesread and check the sum
before you write to your local TEMP file
handle, TEMPHANDLE
open TEMPHANDLE "> ./temp/uploaded.file" or die "failed to write temp file: $!\n"
You can also use POST_MAX to limit upload size. use Carp 'fatalsToBrowser'
to complain to the user if file is too big.
without that, user will get a 500 server error. As long as you know what's going on.
my $sizeLimitInK = 1024;
$CGI::POST_MAX = 1024 * $sizeLimitInK;
0
 
LVL 10

Accepted Solution

by:
makerp earned 100 total points
Comment Utility
# wriiten by paul maker 2000
#
# example to demonstate uploading a file via a file contol on a web page
# * no validation is made on file type/size etc.....
# you will want to validate size to stop denial of service attacks etc
#
# use the cgi package
use CGI;
# create a cgi object
$query = new CGI;

# if we have no parameters then display the form
if(!$query->param())
{
     print $query->header();
     print("
          <FORM ACTION=upload_test.pl METHOD=POST ENCTYPE=multipart/form-data>
               Enter your name and select a file for upload :
               <INPUT TYPE=TEXT NAME=name>
               <INPUT TYPE=FILE NAME=file>
               <BR>
               <INPUT TYPE=SUBMIT VALUE=upload>
          </FORM>
     ");
}
else
{
     # get the name the user entered on the form
     $name = $query->param('name')
     # get the filename (this is also a filehandle as will be demonstated shortly)
     $filename = $query->param('file');

     # spilt it so we can get the filename and extension
     @parts = split /(\\)|(\/)/,$filename;
     # get the last item from the array
     $save_name = @parts[$#parts];
     # open the file (make sure the web server account has write permissions on the directory)
     open(OUTFILE,">".$save_name) or die("cannt copy image over ! ... ".$!);
     # only set binmode if you are sure a binary file is coming over
     binmode OUTFILE;
     # use the filehandle $filename read it in and write it out to our OUTFILE
     while ($bytesread = read($filename,$buffer,1024))
     {
          print OUTFILE $buffer;
     }
     # redirect them to our form
     print $query->redirect('upload_test.pl');
}
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now