ido50 has asked for the wisdom of the Perl Monks concerning the following question:
Hi guys!
I wanna use CGI::Upload to allow file uploading to the server in a file-manager I wrote. I set up a form (Please note that I don't set up the form using cgi.pm objects, just plain html) like this:
Now my problem is that I don't really know how to proccess it to upload the file. I used a little example and wrote this little function:
On testing the script, I get the error "Malformed multipart POST".
I will be much obliged if someone helps me with this problem and guides me through the steps of writing a script that actually uploades a gif file to the server.
Thanks,
Ido Perelmutter,
Israel,
ido@bnei-yehoda.co.il
I wanna use CGI::Upload to allow file uploading to the server in a file-manager I wrote. I set up a form (Please note that I don't set up the form using cgi.pm objects, just plain html) like this:
In here $cur is the current working directory, or in other words, the directory to which the file should be uploaded.<form method="POST" enctype="multipart/form-data" action="url_to_scrip +t.pl?upload"><input type="hidden" name="directory" value="$cur"><br> <input type="file" name="upload" size="20"><br><input type="submit" +value="send"><input type="reset" value="reset"> </form><br><br>
Now my problem is that I don't really know how to proccess it to upload the file. I used a little example and wrote this little function:
As you can see my function takes the file, which should be a gif image, and reads the contents of the temp file created by CGI::Upload. After reading the entire file, I create a new image file and print the contents read before into this file. I don't really know much about how image files work so I wasn't sure that this was valid, but I tried anyway.sub upload { read (STDIN, $in, $ENV{'CONTENT_LENGTH'}); @pairs = split (/&/, $in); foreach $pair (@pairs) { ($name, $value) = split (/=/, $pair); $name =~ s/\+/ /g; $name =~ s/%(..)/pack("C", hex($1))/ge; $value =~ s/\+/ /g; $value =~ s/%(..)/pack("C", hex($1))/ge; $value =~s/<!--(.|\n)*-->//g; if ($parse{$name}) { $parse{$name} .= ", $value"; } else { $parse{$name} = $value; } } my $cgi = CGI->new; my $upload = CGI::Upload->new( $cgi ); if ( $upload->mime_type('upload') eq 'image/gif' ) { my $buffer; my @results; my $fh = $upload->file_handle('upload'); while (read($fh, $buffer, 45)) { push (@results, pack("u", $buffer)); } open (FILE, "+>>$parse{directory}/$upload->file_name('upload')") o +r die "Can't open image file: $!"; for $line (@results) { print FILE "$line"; } close (FILE) or die "Can't close image file: $!"; $fh->close; } filemgr($parse{directory}); # Print directory tree }
On testing the script, I get the error "Malformed multipart POST".
I will be much obliged if someone helps me with this problem and guides me through the steps of writing a script that actually uploades a gif file to the server.
Thanks,
Ido Perelmutter,
Israel,
ido@bnei-yehoda.co.il
20030729 Edit by Corion: Added CODE tags
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Need help with CGI::Upload
by cfreak (Chaplain) on Jul 29, 2003 at 13:39 UTC | |
by jdavidboyd (Friar) on Jul 29, 2003 at 18:51 UTC | |
Re: Need help with CGI::Upload
by tcf22 (Priest) on Jul 29, 2003 at 13:27 UTC | |
by jeffa (Bishop) on Jul 29, 2003 at 14:26 UTC | |
Re: Need help with CGI::Upload
by ido50 (Scribe) on Jul 30, 2003 at 14:10 UTC |
Back to
Seekers of Perl Wisdom