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

Hi fellow monks, Im currently working on a project for a bio-computing problem but im having a problem with uploading a file from the users home dir via web page. I have a cgi script to run as the home page with a file upload box. the code in this html file contains:
print("<form action=Project1.cgi method=POST ENCTYPE=\"multipart\form- +data\">");
which then when executed calls up Project1.cgi. Im new to this game so i need some help.... I cant seem to open the file the user chooses. the code for Project 1 is given below. Any help given is taken with an enlightened thanks.
######## # not sure about this my @file =(); my $html = new CGI; my $filehandle = $html->upload('upload'); my $filename = $html->param('upload'); my $line; my @cols; ######## # read through the file and get the sequence sub checkFile() { # print("The path name was: $file"); print("Opening File..."); open( FILE, "<$filehandle") or die("Error opening the file<BR>"); print("File is now open"); @file = <FILE>; print("<BR><BR> @file<BR><BR>"); while($line = <FILE>) { chomp($line); if($line =~ /([^ACGTacgt])/) {next;} else { @cols = split(/\s/, $line); print("@cols"); } close( FILE ) or die("Error closing the file<BR>"); print("File is now closed"); } }
Cheers fellow monk buddies.

Replies are listed 'Best First'.
Re: Upload a file from home dir
by eieio (Pilgrim) on Apr 01, 2005 at 14:44 UTC
    The CGI upload function returns a file handle, not a file name. Therefore, you don't need to open it, you can just read directly from it. From CGI:

    When called with the name of an upload field, *upload()* returns a filehandle, or undef if the parameter is not a valid filehandle.

    $fh = $query->upload('uploaded_file');
    while (<$fh>) {
       print;
    }
    
      Cheers, So i take it that i just ahve to code to search for stuff i need like regEx's i.e. can now be used instead sub checkFile() { print("File is now open"); @file = <FILE>; print("

      @file

      "); while($line = <FILE>) { chomp($line); if($line =~ /(^ACGTacgt)/) {next;} else { @cols = split(/\s/, $line); print("@cols"); } print("File is now closed"); } }
        Well, I'm not entirely sure what you are trying to do. First, you would use the $filehandle rather than FILE when reading from the file. Second, you are trying to read the entire file twice from the same file handle which won't work. If you want to print the entire contents of the file and then possibly print some additional information based on the regular expression, the following code may help (though I haven't tested it):
        sub checkFile() { my @file = <$filehandle>; print( "&lt;BR&gt;&lt;BR&gt;@file&lt;BR&gt;&lt;BR&gt;" ); foreach my $line ( @file ) { chomp( $line ); if( $line =~ /([^ACGTacgt])/ ) { next; } else { my @cols = split( /\s/, $line ); print( "@cols" ); } } }
Re: Upload a file from home dir
by tlm (Prior) on Apr 01, 2005 at 14:59 UTC

    As posted your code is almost unreadable. (When you post code to PM, put it between <code></code> tags. Better yet, become familiar with the contents of the PM site FAQ, especially Writeup Formatting Tips.) Rather than try reading through it, here's a simple file upload script:

    #!/usr/bin/perl -T -- use strict; use warnings; use CGI qw(:standard); print header, start_html('Upload example'); print_form() unless param; print_results() if param; print end_html; sub print_form { print h2('What file do you have for me?'), start_multipart_form(), filefield(-name => 'upload', -size=>60), submit(-label => 'Do it!'), end_form; } sub print_results { my $output_file = '../whatever'; my $file = upload('upload'); print h2('Bummer!...') and return unless $file and open my $out, ">$output_file"; { my $data; print $out $data while read $file, $data, 1024; } close $out or print h2('Not all is well...') and return; print h1('Success!'); }
    Make sure that the script has permission to write to $output_file.

    Update: Fixed s/param/upload/ in print_results; the original works but the revision uses the "recommended idiom" (see perldoc CGI for a discussion on this).

    the lowliest monk

      Ah, i see... thanks for the hint on txt formatting. Theres me rushing into things again. You learn something new everyday. I hope this is more readable. Thanks for your example.
      MonkPaul.
      sub checkFile() { # print("The path name was: $file"); print("Opening File..."); print("File is now open"); @file = <FILE>; print("<BR><BR> @file<BR><BR>"); while($line = <FILE>) { chomp($line); if($line =~ /([^ACGTacgt])/) {next;} else { @cols = split(/\s/, $line); print("@cols"); } print("File is now closed"); } }
Re: Upload a file from home dir
by chromatic (Archbishop) on Apr 01, 2005 at 18:43 UTC
    @file = <FILE>; print("<BR><BR> @file<BR><BR>"); while($line = <FILE>) ...

    Which do you want to do? Do you want to read all of the lines into @file or do you want to iterate over the file one line at a time? You can't do both.

    I suggest to drop those first two lines, after you fix the open issue.