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:

<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>
In here $cur is the current working directory, or in other words, the directory to which the file should be uploaded.

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:

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 }
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.

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


In reply to Need help with CGI::Upload by ido50

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.