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

Hi monks, I am having problems just simply printing the contents of a file to screen that a user has submitted on my CGI. Can anyone see where i'm going wrong?
#! /usr/bin/perl -w use CGI qw(: standard); my $query = new CGI; print STDOUT $query->header(); print STDOUT $query->start_html -title=> "CGI", -BGCOLOR=>"#CCDDFF"); my $file_contents = $query->param('file'); my $image = $query->param('image'); $file_contents =~ m/^.*(\\|\/)(.*)/; my $name = $2; $image =~ m/^.*(\\|\/) (.*)/; my $image_name = $2; while (<$file_contents>) { $data = do { local $/ = undef; <$file_contents> }; } print STDOUT "$data<p>";

Replies are listed 'Best First'.
Re: submitting files to a cgi
by Aristotle (Chancellor) on Jan 27, 2003 at 17:11 UTC
    You are probably destroying the filehandle/filename duality of $file_contents by applying the regex to it, but I don't really know as I've always stuck to using strict and doing something like.
    my $fh = $query->upload('uploaded_file'); # ... while(<$fh>) { # ... }
    Why are you writing a while loop when you're slurping the file anyway? It will suffice to
    my $data = do { local $/; <$fh> };

    Makeshifts last the longest.

      Hi monks, I am still having real problems uploading the contents of a file from a CGI. I have made alterations to my code based on the suggestions but mothing works. There is no problem with the file, just nothing prints to the screen. Any other suggestions would be much appreciated. ;-)
Re: submitting files to a cgi
by cidaris (Friar) on Jan 27, 2003 at 22:22 UTC
    Is the missing left parens a typo? Also, I try to use single quotes as opposed to double-quotes, though I doubt that is your problem.
    print STDOUT $query->start_html(-title=>'CGI', -BGCOLOR=>'#CCDDFF');

    cidaris