Solved

saving an uploaded file

Posted on 2000-04-09
2
228 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cpan issue 1 64
Using Perl DBI to query oracle 3 43
Regular Expression question to filter with negation. 6 96
Writing a parser for java language 4 80
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

832 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