in reply to Re: Re (tilly) 8: Quick Regex question
in thread Quick Regex question

In this script nothing is done with uploaded files except echo data back to screen. Where I am reading from $handle you would need to open up a file, and print that. You will need to open a file to print to, if you are on windows binmode one or both filehandles (dunno off of the top of my head nor do I have Windows to test with), then:
my $buffer; while (read($handle, $buffer, 10240)) { print IMAGE $buffer; }
to write it out.

Replies are listed 'Best First'.
Re: Re (tilly) 10: Quick Regex question
by Stamp_Guy (Monk) on Jan 15, 2001 at 01:10 UTC
    Ok Tilly, this is where I'm a little sketchy. Here's what I did with what you told me. What did I do wrong?
    #!/usr/bin/perl -T use strict; use CGI qw(:standard upload); use HTML::Entities; print header(), start_html('Upload Test'), h1('Upload Test'), start_multipart_form(), "Enter how many files to upload: ", textfield("filecount"), br(); for my $i (1..param('filecount')) { print "File $i: ", filefield(-name => "file$i"), br(); } print submit(); for my $file (sort grep /file/, param()) { print p(); my $handle = upload($file); open (IMAGE, ">$file"); my $buffer; while (read($handle, $buffer, 10240)) { print IMAGE $buffer; } unless (defined($handle)) { if ($file =~ /(\d+)/) { print h3("File request $1 did not return a handle\n"); } next; } print p(), h3("Uploaded $handle"), br(), "<pre>"; print encode_entities($_) while <$handle>; print "</pre>"; } close(IMAGE);
      Hey Tilly, Here's what I've tried:
      #!/usr/bin/perl -T use strict; use CGI qw(:standard upload); use CGI::Carp; use HTML::Entities; print header(), start_html('Upload Test'), h1('Upload Test'), start_multipart_form(), "Enter how many files to upload: ", textfield("filecount"), br(); for my $i (1..param('filecount')) { print "File $i: ", filefield(-name => "file$i"), br(); } print submit(); for my $file (sort grep /file/, param()) { print p(); my $handle = upload($file); open (IMAGE, ">$1") || die "Couldn't open image file"; # I've tried the following variables for the filename: # $1, $handle, $file, $file$i. # I've also tried a generic filename (ie. test.txt) # I made sure that I put an absolute path name in and # set the permissions to 0777. my $buffer; while (read($handle, $buffer, 10240)) { print IMAGE $buffer; } unless (defined($handle)) { if ($file =~ /(\d+)/) { print h3("File request $1 did not return a handle\n"); } next; } print p(), h3("Uploaded $handle"), br(), "<pre>"; print encode_entities($_) while <$handle>; print "</pre>"; } close(IMAGE);
      What did I do wrong and what else can I try? All that happens is the page gets reloaded and the file field goes blank each time.