rustybar has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have encounter 1 problem in uploading files using perl cgi and I hope you guys can advise me on it.

I keep having this Malformed multipart POST error when uploading files like jpg, gif (binary files). However, when I upload text files, it works. Can I know what could be the problem?

My code for the upload.cgi is shown below:

#!/usr/bin/perl -wT use strict; use CGI; use CGI::Carp qw ( fatalsToBrowser ); use File::Basename; $CGI::POST_MAX = 1024 * 5000; my $safe_filename_characters = "a-zA-Z0-9_.-"; my $upload_dir = "/home/nanyang/Codes"; my $query = new CGI; my $filename = $query->param("file"); if ( !$filename ) { print $query->header ( ); print "There was a problem uploading your file."; exit; } my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); $filename = $name . $extension; $filename =~ tr/ /_/; $filename =~ s/[^$safe_filename_characters]//g; if ( $filename =~ /^([$safe_filename_characters]+)$/ ) { $filename = $1; } else { die "Filename contains invalid characters"; } my $upload_filehandle = $query->upload("file"); open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!"; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE;

for the other page which prompts the user to upload file, the code is similar to this:

<form name="frmUpload_File" method="POST" action="upload.cgi" enctype= +"multipart/form-data"> File to Upload: <input type="file" name="file" /> <p><input type="submit" name="submit" value="Submit" />

Being stuck on it for a whole day. Please advise!

Thanks!

Replies are listed 'Best First'.
Re: Upoading file using perl
by leocharre (Priest) on Feb 09, 2009 at 13:48 UTC

    What is the problem? Are you getting an error message? What is the error message? (as in copy and paste)

    Maybe there is no error message, but you check out the uploaded data and it's corrupt? (??)

      The error message is this one: Malformed multipart POST.

      I can't get any uploaded data at all. The moment I click submit. It directs me to a blank web page with the error message.

        740794 might help.. also, can you tail the erorr log on apache?
Re: Upoading file using perl
by poolpi (Hermit) on Feb 10, 2009 at 13:17 UTC

    You may check errors whith the cgi_error function

    See Retrieving CGI errors

    # From the doc my $error = $q->cgi_error; if ($error) { print $q->header(-status => $error), $q->start_html('Problems'), $q->h2('Request not processed'), $q->strong($error); exit 0; }

    Your $CGI::POST_MAX is 1024 * 5000 bytes.
    Is it big enough for image files?


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb