Solved

saving an uploaded file

Posted on 2000-04-09
2
216 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
ID: 2698289
$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
ID: 2699542
# 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

911 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

19 Experts available now in Live!

Get 1:1 Help Now