Here's some code I used for a similar page I wrote once: the subroutine make_form makes the form the user fills out, and the upload_picture uploads the picture and writes it to the server's disk.

The important snippets are:

  • $page->start_form ( -enctype => 'multipart/form-data' ) (necessary for the upload to work correctly)
  • $page->filefield ( 'picture' ) (creates the field for the user to input their imagename)
  • my $fh = $page->upload ( 'picture' ) or die "can't upload picture!"; (returns a filehandle to the user's image, which can now be used to write the image to the server)

    our @textfields = qw/Name Address City State Zipcode Phone Email/; sub make_form { my $page = shift () or die "no CGI object in make_form"; my $table = new HTML::Table ( -cols => 2 ); $table->addRow ( $page->strong ( 'Your Picture:' ), $page->filefield ( 'picture' ) ); $table->addRow ( $page->strong ( 'Order Type' ), $page->radio_group ( -name => 'order_type', -values => [ 'button', 'magnet' ], -rows => 1, -columns => 2, ) ); $table->addRow ( $page->strong ( $_ ), $page->textfield ( $_ ) ) foreach @textfields; my $form = $page->start_form ( -enctype => 'multipart/form-data' ) + . $table . $page->p . $page->submit ( 'place_order', 'Submit Order' ) . $page->end_form; return $form; } sub upload_picture { my $page = shift () or die "no page in upload_picture"; my $prefix = shift () or die "no prefix in upload_picture"; my $fh = $page->upload ( 'picture' ) or die "can't upload picture!"; my $img_fname = (split ( /\/|\\/, $page->param ( 'picture' ) ) )[- +1]; $img_fname = $prefix . "_" . $img_fname; open ( my $out_fh, '>', $img_fname ) or die "can't open output file in upload_picture: $!"; print $out_fh $_ while (<$fh>); close $out_fh or die "can't close output in upload_picture"; return $img_fname; }

    In reply to Re: Picture upload script! by kesterkester
    in thread File upload script printing to closed filehandle by Anonymous Monk

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.