Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old November 17th, 2000, 06:28 AM
#6 #6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: Melbourne, Victoria, Australia
Posts: 104 #6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to #6
Hi,

I need to make a perl script to receive a form that contains a file upload field amongst other fields.

From a bit of reading, I've established that using some CGI.pm magic would probably be the easiest way to go (correct me if otherwise).

I read the CGI.pm page at search.cpan.org and there was no heading of file-uploading.

Can anyone direct me to a quick start on how to receive and save files from file-upload fields in forms?

Thanks,

Josh

Reply With Quote
  #2  
Old November 17th, 2000, 12:00 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>how to receive and save files from file-upload fields in forms?

This is what I do without CGI.pm...

#!/usr/local/bin/perl

$img_dir = "/path/to/images";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|n)*-->//g;
$value =~ s/<([^>]|n)*>//g;
$value =~ s/^s+//;
$value =~ s/s+$//;
$FORM{$name} = $value;
}
# Generate a 20 chars random image name
@chars=("a".."n","p".."z",1..9);
$image_name="";
for ($i=1;$i<=20;$i++) {
$image_name .= $chars[rand(@chars)];
}

$| = 1;
$buffer =~ /^(.+)rn/;
$bound = $1;
@pairs = split(/$bound/,$buffer);
@var = split(/rn/,$pairs[2]);
@type = split(///,$var[2]);
$image_extension = "$type[1]";

# you can do this to see what those variables are and decide what to use
#print "Content-type: text/htmlnn";
#print "$pairs[0] - $pairs[0]<br>n";
#print "$pairs[1] - $pairs[1]<br>n";
#print "$pairs[2] - $pairs[2]<br>n";
#print "$var[0] - $var[0]<br>n";
#print "$var[1] - $var[1]<br>n";
#print "$var[2] - $var[2]<br>n";
#print "$type[0] - $type[0]<br>n";
#print "$type[1] - $type[1]<br>n";
#print "$type[2] - $type[2]<br>n";

if ($pairs[2] =~ /Content-Type:/) {
$pairs[2] =~ s/^rn.+filename.+[^w.%-]([w.%-]+)"rn(.*rn)rn//;
$pairs[2] =~ s/rn$//;
$image_file = "$pairs[2]";
}
else {
print "Content-type: text/htmlnn";
print "Error: Can't determine Content-Typen";
exit(0);
}
# Getting variable from another form field
if ($pairs[1] =~ /another_form_field/) {
$pairs[1] =~ s/(.*)another_form_field"rn(.*rn)rn//;
$pairs[1] =~ s/rn$//;
$pairs[1] =~ s/rn//;
$pairs[1] =~ s/(.*)another_form_field"s+//;
}

# Saving the image file
open(OUTPUT,">$img_dir/$image_name.$image_extension");
print OUTPUT $image_file;
close(OUTPUT);
chmod (0666,"$img_dir/$image_name.$image_extension");

print "Content-type: text/htmlnn";
print "Done!n";

Reply With Quote
  #3  
Old November 17th, 2000, 06:31 PM
#6 #6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: Melbourne, Victoria, Australia
Posts: 104 #6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to #6
Hmmn... I understand the code you posted, but I would prefer a "straighter" way.

Has anyone done it with cgi.pm?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > File Uploading


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway