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

Brothers and Sisters. I have read the upload section in CGI.pm but it seems I'm missing something. Perhaps a kind monk could show me the path toward enlightenment:

# strict, warning and other things snipped. $posid = param('posid'); $name = param('name'); $phone = param('phone'); $email = param('email'); $resumeurl = param('resumeurl') || "none"; $resumefilename = upload('resumefile') || "none"; $resumefiletype = uploadInfo($resumefilename)->{'Content-Type'}; #check for correct file type foreach $x (@filetype){ if ($x eq $resumefiletype){ $notype++; } } if ($notype == 4) { die "Sorry we do not accept $resumefiletype files. +"; } while (<$resumefilename>){ $resumefile .= $_; } #insert into resumes $statement = "INSERT INTO resumes (appid, posid, resumefilename, resum +efiletype, resumefile, resumeurl) VALUES (?, ?, ?, ?, ?, ?)"; $sth = $dbh->prepare($statement) || die "Could not prepare statement: +".$dbh->errstr; $sth->execute($appid, $posid, $resumefilename, $resumefiletype, $resum +efile, $resumeurl) || die "Could not prepare statement: ".$dbh->errst +r;
My webserver is Apache running on Linux.

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: CGI: Uploaded binary file is corrupted
by davorg (Chancellor) on Oct 30, 2002 at 15:57 UTC
      I tried binmode $resumefilename, ":raw" before the while loop but there was no change in the results.

      Neil Watson
      watson-wilson.ca

Re: CGI: Uploaded binary file is corrupted
by iburrell (Chaplain) on Oct 30, 2002 at 17:51 UTC
    The CGI part looks correct. One problem is that you are reading the file line by line. If it is binary file, you want to read it all in one go. Make sure to check that the content-length is valid and you read the full length. Either read or brackets with $/ undefined will work.
    read $fh, $contents, $length; $/ = undef; $contents = <$fh>;
    The way to debug the problem is check that the file is valid at various stages of process. Check the file before upload, check the temporary file, check the contents read into memory, check the value in the database.
      Turns out I'm and idiot. I had the database field set to blob, which was to small. Mediumblob works well.

      Thanks for the help.

      Neil Watson
      watson-wilson.ca

Re: CGI: Uploaded binary file is corrupted
by jsprat (Curate) on Oct 30, 2002 at 17:22 UTC
    Try this instead of the line by line while loop when you are reading binary data:

    while (my $bytes = read($resumefilename, my $buffer, 1024)) { $resumefile .= $buffer; }
Re: CGI: Uploaded binary file is corrupted
by fglock (Vicar) on Oct 30, 2002 at 17:52 UTC

    Did you try saving $resumefile as a normal file instead? It might be a database issue.