Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

saving an uploaded file

Posted on 2000-04-09
2
Medium Priority
?
253 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 400 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

916 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