You can only run this code from another form that provides the filename and handle, without them you get the error. I've added a simple form to your script to prove it works OK
#!perl -wT use CGI qw(:standard); #### be sure to change this use CGI::Carp qw ( fatalsToBrowser ); use strict; use Fcntl qw ( :DEFAULT :flock ); use constant UPLOAD_DIR => "c:/upload/"; my $q = new CGI; my $filename = $q->param('file'); my $fh = $q->upload('file'); if ($fh){ #### check for filehandle $filename =~ s/[^\w.-]/_/g; if ($filename =~ /^(\w[\w.-]*)/) { $filename = $1; } else { error($q, "Invalid file name." ); } my $upload_dir = UPLOAD_DIR . $filename; sysopen(OUTPUT, $upload_dir, O_CREAT | O_RDWR | O_EXCL); my $buffer = ""; while (read($fh, $buffer, 16384)) { print OUTPUT $buffer; } close OUTPUT; print $q->header, $q->start_html("At long last"), $q->h1("done dude...doh"), #### add button to reset form $q->start_form( -action => url () ), $q->submit ( -name=>"done", -value=>"Done"), $q->end_form(); #### $q->end_html; #### simple form to choose file to upload } else { print $q->header, $q->start_html("Upload File"), $q->start_multipart_form( -action => url () ), "File : ", $q->filefield (-name=>"file", -size=>60), $q->submit ( -name=>"submit", -value=>"Submit"), $q->end_form(); $q->end_html; }
poj

In reply to Re: file upload (again) by poj
in thread file upload (again) by r_mehmed

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.