Please help me upload a binary file through a CGI script.

Mark Steele msteele at
Wed Nov 27 16:35:09 CST 2002

hey all Mongers,

I'm trying to write a Perl CGI script that will upload an Excel file
using Apache on a HP-UX system.

I've been using some of the code from the perldoc part of and
some code from the O'Reilly book "CGI Programming", I'm lost...

All I want is a CGI script which will be called from a web page link
that will upload Excel files (or binary file in general), text files 
would be nice too, but at a minimum just binary files.

I can get text files fine.

I get this in the Apache error log:

[Wed Nov 27 14:34:56 2002] [error] [client] Premature end of
script headers: /opt/apache2/lib/cgi-bin/, referer:
[Wed Nov 27 14:34:56 2002] [error] [client] Server
closed socket during multipart read (client aborted?)., referer:

Can someone point me to some existing code that does this? Or for a laugh
you can look at this mess I've created. As you can see I've tried lots of

Thanks for ANY help....


#!/opt/perl5/bin/perl -w

use CGI;

$q = new CGI;

print $q->header();
print $q->start_html(-title=>'Get Excel file'), "\n";

print $q->start_multipart_form();
print "Select Excel file:<br>","\n";
print $q->filefield(-name=>'uploaded_file',
                   -default=>'starting value',
                   -size=>50), "\n";
print "<br><br>";
print $q->submit(-name=>'Submit',
                 -value=>'Get File'), "\n";

print $q->end_form;
$filename = $q->param('uploaded_file');
print "<br>";
print "filename = $filename";
print "<br>";
$buffer = "";

#binmode $fh;
#binmode OUTPUT;

open (OUTFILE,">>/tmp/mark");
while ($bytesread=read($filename,$buffer,1024)) {
   print OUTFILE $buffer;

#$fh = $q->upload('uploaded_file');
#while (<$fh>) {
#  print OUTFILE;

#$ref = $q->uploadInfo($filename);

$file_type = $q->uploadInfo($filename)->{'Content-Type'};

print "file_type = $file_type";

#while ( ($key1,$value1) = each %{$ref} ) {
#  print "KEY: $key1 VALUE: $value1","<br><br>";

#foreach $key ($q->uploadInfo($filename)) {
#  print "$key";

#$type = $q->uploadInfo($filename)->{'Content-Type'};
#print "Type: $type","<br>";

#unless ($type eq 'text/html') {
#   die "HTML FILES ONLY!";

#if ($q->param() ) {
#   $filename = $q->param('uploaded_file');
#   print "$filename","<br>";
#print "$filename","<br>";

print $q->end_html();

#<form method="post" action="../cgi-bin/upload-1.cgi"
#  Choose file to upload:
#  <input type="file" name="file" size="50"/>
#  <br>
#  Enter filename: <input type="text" name="filename" size="20"/>
#  <p><input type="submit" name="Upload" value="Upload File"/></p>


The posting address is: san-diego-pm-list at

List requests should be sent to: majordomo at

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at> with the following
command in the body of your email message:

    unsubscribe san-diego-pm-list

If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at> .
This is the general rule for most mailing lists when you need
to contact a human.

More information about the San-Diego-pm mailing list