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

Hello

I am trying to upload a word doc and a jpg image using CGI. The problem is that the word doc says "The document name or path is not valid." And when I upload a jpg it comes out all garbled. Works fine if I use a bmp. I think I'm just missing something. Pretty new to this.

Thanks in advance!

#! C:\perl\bin\perl -w use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); my ($i, $line, $t_name, $name, $ver, $comments, $comments_new, $buffer +, $bytesread); my (@fileinput, @table_dat, @data, @fileinput_ap, @comments, @test); # Some Security Holes Plugged $CGI::POST_MAX = 100 * 1024 * 1024; # limit posts to 100M max my $q = new CGI; ######################################### # Get data from input and Untaint # ######################################### $t_name = $q->param('ap_name') or die "Cannot find name: $!"; if ( $t_name =~ /^([a-zA-Z\d_\s]+)$/ ) { $name = $1 } else { print "Content-type: text/html\n\n"; print "Only letters, numbers, underscores and whitespaces are allo +wed in the name field!!"; exit; } ################# # Upload images # ################# # Image my $file_name01 = $q->param('name_upfile01'); my $file01 = $q->upload('upfile01') or die "test:$!"; # Word doc my $file_name02 = $q->param('name_upfile02'); my $file02 = $q->upload('upfile02') or die "test:$!"; mkdir "C:/web/aps/mi/$name", 0755 or die "Cannot make dir: $!"; open (OUTFILE, ">>C:/web/aps/mi/$name/$file_name01") or die "Cannot op +en dir $name: $!"; while ($bytesread=read($file01,$buffer,1024)) { print OUTFILE $buffer; } close OUTFILE; open (OUTFILE2, ">>C:/web/aps/mi/$name/$file_name02") or die "Cannot o +pen dir $name: $!"; while (<$file02>) { print OUTFILE; } close OUTFILE2;

Replies are listed 'Best First'.
Re: Trouble Uploading Word Doc and JPG
by eXile (Priest) on Apr 14, 2004 at 00:49 UTC
    don't you need binmode on your filehandles for this?

    Also I think the '>>' in the 'open'-statements you use causes data to be appended to an existing file. If the file doesn't exist this seems no problem (your file will be created), but if the file exists you'll get 2 files glued together. Better to use '>' so the file will be created (and overwritten), and even better to check if a file already exists before you try to write it.
Re: Trouble Uploading Word Doc and JPG
by Util (Priest) on Apr 14, 2004 at 02:18 UTC
    • In your final loop, you are printing to OUTFILE, when you need to print to OUTFILE2.
    • As eXile noted, on Win32, you *do* need to binmode any filehandles on non-text files to prevent corruption or truncation.
    • As eXile noted, you need open your output files in truncate (>) mode. You are getting away with using append (>>) mode only because you first die if the directory exists. Fix this, or it will bite you someday.
    • While you are at it, change to the three-argument form of open. Someday you might receive a filename that interacts poorly with your current "magic open".
    • You should add 'or die' to your close statements on the output files, in case your filesystem fills up.