Click to See Complete Forum and Search --> : accepting file uploads with perl?
DJRobThaMan
11-09-2005, 06:41 PM
So now I've got some way alopng with making my website editable and I need some more help, cuz I suck. I want the user to be able to upload a file, and I'm not sure how to write the perl to accept it. I thought maybe I could just load it into a variable open a new file and say print newfile $file_variable, but that didn't work. Anybody know how to write this script or where I could get some nudges in the right direction?
Thanks
DJRobThaMan
11-10-2005, 12:29 AM
I'm sorry. Should have tried a bit harder before I posted. Just had to make a few adjustments and it works now.
Douglas
DJRobThaMan
11-10-2005, 10:43 AM
I spoke a bit too quickly it seems. The perl code I've written creates a new filie with the same name as the one being uploaded but doesn't actually save any data to it. I've looked around and the code I have apparently is supposed to be able to work. Here it is:
#!/www/perl/bin/perl.exe
use CGI qw(:standard);
use strict;
my $file = param('file');
$file =~ s/.*[\/\\](.*)/$1/;
my $handle = upload('file');
open(doc, ">", "../webroot/$file")
or die $!;
binmode(doc);
while(<$handle>)
{
print doc $_;
}
close(doc);
print header();
print "hopefully it uploaded";
The only thing I see around the net that is a bit different is that they define a variable so:
my $cgi = new CGI;
and use it like so:
my $file = $cgi->param('file');
for all the param, upload, and header statements.
Anybody know what I'm doing wrong?
The perl code I've written creates a new filie with the same name as the one being uploaded but doesn't actually save any data to it.
This scenario is generally the result of the incorrect enctype used in the upload form. Make sure the form uses the following:
<form enctype="multipart/form-data"... etc...
DJRobThaMan
11-10-2005, 06:46 PM
So, I realize now how dumb I am. I actually had the enctype but I ended the form incorrectly. Instead of </form> I had </forn>. I hate when I make simple mistakes like that. Anyway, I changed it and it works now.
Thanks for the help.
You're welcome.
I do have a couple of issues with your explanation, though. I tested the upload with the closing form tag using : </forn> and even </f> with no ill-effects. I also tested NOT using the "multipart/form-data" enctype and as you alluded to prior, the file was created, but contained no "data". So, I can't explain why you had different results if you had indeed used the proper enctype originally. ???
DJRobThaMan
11-11-2005, 12:54 AM
Well, when I changed the closing tag I also pasted the enctype from your post and replaced the one I had already written. I didn't notice any difference, but maybe there was a spelling error in there as well. I suppose that would make sense. Other than that I couldn't really see what it could be because I don't think I changed anything else. Definitely nothing in the perl script.
...but maybe there was a spelling error in there as well... Yep... that would explain it.