[lug] perl question

Hugh Brown hugh at math.byu.edu
Sun Apr 9 15:48:18 MDT 2000


Thanks for all of the replies.

Below follows the code that I am fiddling with.  I asked about this
earlier but wasn't specific enough in my query.  This is a cgi-script to
allow uploading to a server.  I think I need to look into the seek
command that Michael wrote about.  My solution was to combine the two
while loops.  I was curious to know how to read through a file again for
my own benefit.

Hugh

> Without the actual code, I can't really tell.  Is it a scope issue?
> Are you setting the $filename inside the while loop?  If so, the
> variable would go away at the last '}' at the end of the while loop.
> 
> If you wanted to post some more code, maybe people could get a better
> feel for what you're trying to do...


#!/usr/bin/perl
#script: upload.pl

use CGI qw/:standard/;


print header,
        start_html('file upload'),
        h1('file upload');
print_form()    unless param;
print_results() if param;
print end_html;

sub print_form {
    print start_multipart_form(),
    "Filename:",br,
    filefield(-name=>'upload',-size=>40),
    submit(-label=>'Upload File'),
    end_form;
}

sub print_results {
    my $length;
    my $file = param('upload');
    if (!$file) {
       print "No file uploaded.";
       return;
    }
    print h2('File name'),$file;
    print h2('File MIME type'),
    uploadInfo($file)->{'Content-Type'};
    open (OUTFILE,">/home/brownclan/tmp/$file")||die $!;
    while (<$file>) {
       $length += length($_);
    }
    while (<$file>){
       print OUTFILE $_;
    }
    print h2('File length'),$length;

    close(OUTFILE);
}




More information about the LUG mailing list