in reply to CGI file very odd

Yep, first of all, I'll tell you to throw in 'use strict' at the top of the script. This would catch the fact that $name, used on line 17 was not declared anywhere within your script. As well, you are using upload() to get a filehandle, then attempting to extract the filename from it. IIRC, upload() does not return the filename in any way whatsoever, different from the way in which param() works (correct me if I am wrong). The part that confuses me the most is your opening of a file for reading. If you meant to replace $name on line 17 with $file_name, then I see what you were trying to do. With what is returned with param() or upload(), you do not need to open the "file" for reading. CGI already passed you the filehandle, so all you have to do is read from it. Rewriting things to make it work and to pretty it up a bit (untested):

#!/usr/bin/perl -w use strict; use CGI ':standard'; print header(), start_html('CGI Upload'), my $file = param('file'); $file =~ s#^[^/\\]*[/\\]##; #Sufficient for windows and *nix if ($file =~ /[^\w\.\-]/) { print p( strong('Invalid Filename!') ); exit; } my $fh = upload('file'); my $info = do { local $/; <$fh> }; print p($info), end_html;


      C:\>shutdown -s
      >> Could not shut down computer:
      >> Microsoft is logged in remotely.
    

Replies are listed 'Best First'.
Re: Re: CGI file very odd
by Anonymous Monk on Jan 31, 2003 at 10:12 UTC
    WOW!! thanks Coruscate and everyone else who offered their replies, i really really appreciate this. I was getting really quite down over the simplest thing not working but the code posted above is perfect. Many thanks ;-)