Loading ...
Sorry, an error occurred while loading the content.

A file upload script

Expand Messages
  • Charles K. Clarkson
    Hello List, Someone asked if I could post a file upload script. Not ever having written one I immediately replied: YES! Here is a script that uploads a text
    Message 1 of 1 , Apr 28, 2002
    • 0 Attachment
      Hello List,

      Someone asked if I could post a file upload script. Not
      ever having written one I immediately replied: YES!

      Here is a script that uploads a text file and shows it
      to the user. It uses CGI.pm with strictures and warnings.
      Later we'll figure out how to save files to a web site
      and how to do the same with binary files.
      If I get industrious perhaps we'll look at using
      HTML::Template or CGI::Application also. Don't worry
      I won't do all that in one post. This is what I came up
      with after reading the CGI.pm POD. You'll need
      CGI.pm version 2.74 or higher.

      #!/usr/bin/perl
      require v5.6.0;
      use strict;
      use warnings;
      $|++;

      use CGI;
      $CGI::POST_MAX=1024 * 10; # max 10K posts

      use CGI::Carp 'fatalsToBrowser';
      my $q = CGI->new;

      unless ( $q->param ) {
      print default_form();
      exit;
      }

      start_upload();

      sub default_form {
      my $q = CGI->new;
      return
      $q->header,
      $q->start_html( -title => 'Upload your text file' ),
      $q->start_multipart_form,
      $q->h2( 'Upload your text file' ),
      $q->br,
      $q->filefield(
      -name => 'uploaded_file',
      -size => 50,
      ),
      $q->br, $q->br,
      $q->submit( -name => 'View File' ),
      $q->end_form,
      $q->end_html;
      }

      sub start_upload {
      my $q = CGI->new;
      # $local *FH = $q->upload( 'uploaded_file' );
      my $fh = $q->upload( 'uploaded_file' );
      print
      $q->header,
      $q->start_html( -title => 'View File' ),
      $q->pre(<$fh>),
      $q->end_html;
      }

      __END__

      If you're using a pre 5.6 version of perl, you may
      need to change start_upload:

      sub start_upload {
      my $q = CGI->new;
      $local *FH = $q->upload( 'uploaded_file' );
      # my $fh = $q->upload( 'uploaded_file' );
      print
      $q->header,
      $q->start_html( -title => 'View File' ),
      $q->pre(<FH>),
      $q->end_html;
      }

      I tested this on my localhost. It's on a windows machine
      and will probably burp when we start on binary files. I also
      put it on a linux server on the web. (See below)

      I realize this is the first time many of you have been
      introduced to the CGI.pm module. Using $q-> is the OO
      (Object-Oriented) interface. I used the function-oriented
      interface for about six months and now find the OO
      interface nicer. TIMTOWTDI.

      If you have any questions about what's happening here,
      please ask. There are more than 1400 people on the list
      and many others likely have the same question you do.

      Online docs for CGI.pm are at:
      <http://stein.cshl.org/WWW/software/CGI/cgi_docs.html>

      You can test the script online. Large files (>10K) will be ignored.
      <http://www.clarksonenergyhomes.com/cgi-bin/file_upload.pl>

      Next time we'll look at saving the file to the server
      and at binary files.

      HTH,
      Charles K. Clarkson
      --
      Head Bottle Washer,
      Clarkson Energy Homes, Inc.

      CJ Web Work - Domains for Real Estate Investors.
      E Pluribus Unum -- One from many.
    Your message has been successfully submitted and would be delivered to recipients shortly.