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

I have been able to get uploads of files to work correctly, but I have run into a snage. I am unable to get the data to upload into a (PDF)file. The name gets created but it has a 0 bytes. I changed my HTML to ENCTYPE="application/pdf" from ENCTYPE="multipart/form-data". My CGI looks like the following:
#!/usr/bin/perl print "Content-type: text/html\n\n"; print "<HTML><HEAD><TITLE>Upload Script</TITLE></HEAD><BODY>"; use CGI; # load the CGI.pm module my $GET = new CGI; # create a new object my @VAL = $GET->param; #get all form field names foreach(@VAL){ $FORM{$_} = $GET->param($_); # put all fields and values in hash } $uploadfile = $FORM{'userfile'}; $orgfile = $FORM{'userfile'}; $uploadfile =~ s/^.*(\\|\/)//; $uploadfile =~ s/\s+//ig; $uploadfile =~ s/\./PsJsDoT/g; $uploadfile =~ s/\-/PsJsDaSh/g; if($uploadfile =~ /\W/){ $uploadfile =~ s/\W/n/ig; # replace any bad chars with the letter " +n" } $uploadfile =~ s/PsJsDoT/\./g; $uploadfile =~ s/PsJsDaSh/\-/g; $filesize = $FORM{'MAX_FILE_SIZE'}; $denverfile = "../data/denver/"; $chicagofile = "../data/chicago/"; $lafile = "../data/losangelas/"; $sanfile = "../data/sandiago/"; ($city,$date,$name,$ext) = split(/\./,$uploadfile); if ($city eq "denver") { $movefile = $denverfile.$uploadfile; }elsif ($city eq "chicago") { $movefile = $chicagofile.$uploadfile; }elsif ($city eq "losangelas") { $movefile = $lafile.$uploadfile; }elsif ($city eq "sandiago") { $movefile = $sanfile.$uploadfile; }else{ $newfile = "../data/$city/"; mkdir ("../data/$city", 0777); chmod(0777, "../data/$city"); $movefile = $newfile.$uploadfile; } print "The file you uploaded was: $uploadfile\n"; print "The file went to directory: $movefile\n"; open(NEW, ">$movefile") || die "Can not open $movefile"; binmode NEW; #switch to binary mode # start reading users HD 1 kb at a time. while ($bytesread=read($orgfile, $buffer, 1024)) { # print each kb to the new file on the server print "buffer: ".$buffer; print NEW $buffer; } close NEW; # close the new file on the server and we're don +e chmod(0777, "$movefile"); print "</BODY></HTML>"; exit;
but the $buffer always seems to be empty. Can anyone help. Please email me with ideas: dave@ddsis.net or blanscet_d@hotmail.com Thanks for the time. Dave
edited by boo_radley : code tags

Replies are listed 'Best First'.
Re: upload PDF files
by rob_au (Abbot) on Jan 21, 2003 at 08:22 UTC
    In addition to the excellent link provided by Tommy above, I would direct your attention to the CGI::Upload module which is designed to facilitate the uploading of files via HTTP. This module incorporates methods for verifying the content and MIME-type of the uploaded file - For example, the following code incorporates a check to ensure that the uploaded file is a PDF file:

    #!/usr/bin/perl -Tw use CGI; use CGI::Carp qw/ fatalsToBrowser /; use CGI::Upload; use IO::File; use strict; my $upload = CGI::Upload->new; if ( defined $upload->file_name('userfile') ) { unless ( $upload->mime_type('userfile') eq 'application/pdf' ) { # Error handling to be incorporated here which will be trigg +ered if the # uploaded file is _not_ a PDF file die "Uploaded file is not a PDF document file"; } # Open the output binary file stream my $output = IO::File->new( $upload->file_name('userfile') ); die $! unless defined $output; binmode( $output ); # Read the file data from the uploaded file stream and write thi +s data to the # output file stream my $buffer = ''; my $input = $upload->file_handle('userfile'); while ( read( $input, $buffer, 1024 ) ) { print $output $input; } $output->close; chmod 0777, $upload->file_name('userfile'); }

    Further to this, another example of the usage of CGI::Upload can be found here.

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000100000"))'

Re: upload PDF files
by Tommy (Chaplain) on Jan 21, 2003 at 08:11 UTC