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

Hi all, I have a cgi script which requests the user to browse a file for upload and for an email address. This then calls the below code. I would like store a copy of the file in $upload dir and also print the file to the screen. The file is a simple txt file (very small, used to confirm the file. The file is being stored in the same dir as the script and the file is loading but as a single line; no new line characters
$upload_dir = "C:\Program Files\Apache Group\Apache2\cgi-bin\test"; $query = new CGI; $filename = $query->param("data"); $email_address = $query->param("email_address"); $filename =~ s/.*[\/\\](.*)/$1/; $upload_filehandle = $query->upload("data"); open UPLOADFILE, ">$upload_dir/$filename"; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE; open (FH, "<$filename"); print $query->header ( ); print <<end_TEST; <HTML><HEAD><TITLE>Thanks!</HEAD></TITLE> <BODY> <P>Thanks for uploading your data!</P> <P>Your email address: $email_address</P> <P>Your filename: $filename</P> end_TEST foreach $line (<FH>) { print $line; print "\n"; } close (FH); print <<end_TEST;
Thanks </BODY> </HTML> end_TEST

Replies are listed 'Best First'.
Re: cgi print file to screen
by jasonk (Parson) on May 26, 2005 at 14:05 UTC

    You are printing HTML, so if you want newlines you either need to wrap <pre> tags around the output, or put <br> tags at the end of each line.


    We're not surrounded, we're in a target-rich environment!
Re: cgi print file to screen
by Joost (Canon) on May 26, 2005 at 14:45 UTC
      thanks for the helpful html comment; works fine now cheers