If your version of CGI.pm is not up to date then the upload() method will be broken or non existant. 2.47 rings a bell. Here is some code that I have used before that does work!

use CGI; # allow uploads $CGI::DISABLE_UPLOADS = 0; $CGI::POST_MAX = 1,024,000; my $q = new CGI; ..... sub upload_file { no strict 'refs'; # CGI < v2.47 use vvvvvv a symbolic reference f +or the filehandle my $fh = $q->upload('upfile') || $q->param('upfile');; my $buffer; open (OUTFILE, ">$path_to_files/$upname") or die_nice("Unable to create upload output file '$upname': $! +\n"); binmode $fh; binmode OUTFILE; my ($bytes, $read); while ($read = read ($fh, $buffer, 16384)) { print OUTFILE $buffer; $bytes += $read; } close OUTFILE; $bytes /= 1000; return "File '$upname' uploaded. $bytes Kb received successfully.<br +>\n"; }

The main point is that you can use the return value of $q->param('upfile') as a symbolic reference to get a filehandle. Indeed you have to in early version of CGI.pm, thus the or clause. Also note that although CGI.pm allows file uploads by default a wise admin may hack source so that they are denied by default so you should make sure they are enabled. You need to make sure that your submitting form uses the correct ENCTYPE (multipart/form-data) note that the CGI.pm default is "application/x-www-form-urlencoded" so you need to use start__multipart_form so you get a form like:

<FORM METHOD="POST" ACTION="http://somewhere.com/cgi-bin/script.cgi"; ENCTYPE="multipart/form-data"> <INPUT TYPE="file" NAME="upload_file" SIZE="42"> </FORM>

PS You can check your version of CGI.pm like this:

use CGI; print CGI:VERSION;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Uploading a File with CGI.pm by tachyon
in thread Uploading a File with CGI.pm by dru145

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.