Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to upload a file through perl/cgi code

Status
Not open for further replies.

cyan01

Programmer
Mar 13, 2002
143
0
0
US
Hi,

I need to write a perl/cgi code to allow users to upload ascii/jpg files to a remote server through http. Could someone tell me where I can find a sample code? Or could you show me a piece of your sample code?

Thank you so much!
 
Have a look at the FAQ section here, there's at least two good samples there, and also check the CGI section

HTH
--Paul
 
here you go
You can decide where file is to be saved. Sample below save file in directory c:/upload (make sure to create this directory before you test the script)


#!c:/perl/bin/perl.exe

use CGI qw/:standard/;

print header;
if(param) {
save_file();
} else {
print_form();
}

sub print_form {
print start_html('File upload'),
start_multipart_form(),
'File:',filefield(-name=>'file',-size=>60),br,
submit(-label=>'Upload'),
end_form,
end_html;
}

sub save_file {
my $file = param('file');
if (!$file) {
print start_html,"file not found",end_html;
exit;
}
if($file=~m!([^/\\]+$)!) {
my $savefile="c:/upload/$1";
if(open(OUTFILE, ">$savefile")) {
binmode(OUTFILE);
while (my $bytesread = read($file, my $buffer, 16384)) {
print OUTFILE $buffer;
}
close (OUTFILE);
print start_html,"file upload complete, saved as $savefile",end_html;
} else {
print start_html,"error opening file $savefile: $!",end_html;
}
}
}
-----------------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top