in reply to Re: FileParse is NOT working correctly
in thread FileParse is NOT working correctly

This is part of a huge form processing CGI. Nothing comes from command lines, everything comes from form input.

Here is the code that retrieves the file from the form and the email that sends the attachment. The file name appears to have underscores after it is retrieved from the form for some reason????

# ADD ATTACHMENTS TO FORM # Set the attachment variables my $FileName; my $FileName2; my $FileName3; my $myFilePath; my $i; my @attachmentFiles; # Get the attachment $FileName = $query->param('FileName'); $FileName2 = $query->param('FileName2'); $FileName3 = $query->param('FileName3'); @attachmentFiles = (); # Put the attachments in an Array # This allows each file to be filtered through read and attached if( !defined($FileName) || $FileName ne "") { push (@attachmentFiles, $FileName); if( !defined($FileName2) || $FileName2 ne "") { push (@attachmentFiles, $FileName2); if( !defined($FileName3) || $FileName3 ne "") { push (@attachmentFiles, $FileName3); } } }

This pulls in the values from the form. There are three attachment fields so they get pushed to an array, but this problem has been there from the get go when there was only 1 file. My first thought was to simply rename the file with a time stamp, but I would rather retain the name of the file they uploaded.

Here is the code that attaches it to the email. I had worked on this for months and don't understand why the path is being altered so that I can't get the file name easily.

sub admin_mailer { print DEBUG "in admin mailer...\n" if ($debug_log); $admin = $query->param('admin'); # If $email begins "test:" then we are in a test mode. Send # the admin e-mail to the specified e-mail address. if ( $email =~ /^test:/ ) { $email =~ s/^test\://; $admin = $email; } @sendary = split ( /,/, $admin ); if ( $query->param('admin_subject') ) { $subj = convertInputString( $query->param('admin_subject') ); } else { $subj = convertInputString( $query->param('subject') ); } print DEBUG "Admin mailer converting...\n"; my $email = convertInputString( $query->param('EMail') ); my $fname = convertInputString( $query->param('FirstName') ); my $lname = convertInputString( $query->param('LastName') ); my $from = $fname . " " . $lname . " <" . $email . ">"; # # Give the e-mail client a hint of what charset to # expect. Some clients may ignore but at least we # try to behave nicely. # #&logEvent("Mailing admins @sendary From: $fname $lname"); #open( MAIL, "|$MAILPROG @sendary" ) or die "Cannot open $MAILPROG +: $!"; #print MAIL "MIME-Version: 1.0\n"; #if ( $inputCharset =~ /UTF\-8/i ) { # print DEBUG "Formatting Mail UTF-8\n" if ($debug_log); # print MAIL "Content-type: text/plain; charset=UTF-8\n"; #} #else { # print DEBUG "Formatting Mail ISO-8859-1\n" if ($debug_log); # print MAIL "Content-type: text/plain; charset=ISO-8859-1\n"; #} #print MAIL "Content-type: text/plain; charset=" . $outputCharset +. "; \n"; #print MAIL "From: \"$fname $lname\"<$email>\n"; #print MAIL "To: $admin\n"; #print MAIL "Subject: $subj \n"; #print MAIL "\n"; #print MAIL ""; @names = $query->param; @namelist = split ( /,/, $query->param(admin_mailer_fields) ); print DEBUG "namelist : @namelist\n"; if (@namelist) { # Go through the form hash from loadFormHash and get the d +esired fields in the order of # admin_mailer_values foreach $name (@namelist) { $message_body = $message_body . $name . ": " . $HASH{$ +name} . "\n\n"; } } else { foreach $record ( keys(%HASH) ) { $message_body = $message_body . $record . ": " . +$HASH{$record} . "\n\n"; } } ### Create the multipart container $msg = MIME::Lite->new ( From => $from, To => $admin, Subject => $subj, Type =>'multipart/mixed' ) or die "Error creating multipart container: $!\n"; ### Add the text message part $msg->attach ( Type => 'TEXT', Data => $message_body ) or die "Error adding the text message part: $!\n"; ### Add the file if(@attachmentFiles ne "" || @attachmentFiles ne "NULL"){ for ( $i = 0 ; $i < scalar ( @attachmentFiles ) ; $i++ ) { &openattachment; if($theirFile ne "NULL" || $theirFile ne "" ){ $msg->attach ( Type => 'AUTO', Path => $myFilePath, FileName => $FileName, Disposition => 'attachment' ); } } } ### Send the Message $msg->send; }

Replies are listed 'Best First'.
Re^3: FileParse is NOT working correctly
by almut (Canon) on Apr 20, 2009 at 21:00 UTC
    The file name appears to have underscores after it is retrieved from the form

    So you're saying the path already has the underscores when you print out $FileName right after this(?):

    # Get the attachment $FileName = $query->param('FileName');

    If so, it's no surprise fileparse() has issues with it. In this case, the next step would be to investigate what the browser actually sends...

    One general debugging rule is to reduce the case to the smallest piece of code that does exhibit the problem. Why look at the mailing code when the problem is with parsing the filename? Remove everything that likely is unrelated. If the problem persists, it was unrelated. Otherwise, take the code back in stepwise until the problem reappears...  Also, always verify implicit assumptions. I.e. if you suspect that fileparse() is doing something wrong, you're implicitly assuming it's getting proper input. Verify it, print it out.  That way, you can usually narrow down rather quickly on where things are going wrong...

    And if you haven't found the error yourself by then, you've at least produced a small (and hopefully self-contained) piece of code which would allow others to reproduce the issue.