In the following snippet of code:

use CGI; sub write_file { $filename = $query->param('portrait'); open (OUTFILE, "> directory/file.xxx"); while ($bytesread = read ($filename, $buffer, 1024)) { print OUTFILE $buffer; } close $filename; close OUTFILE; }
...the $filename variable is getting set to the value of a <input type="file" name="portrait" field> from a "ENCTYPE=multipart/form-data" form which allowed the user to browse their computer and select a file for upload. No big deal.

What I don't understand is how that $filename variable suddenly becomes a filehandle argument for the read function. Doesn't the $filename contain only the path the user selected on their computer? How the hell does the server upload the file given only this information? There has to be a lot more happening with "ENCTYPE=multipart/form-data" forms that's invisible to me. What is it?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar";
$nysus = $PM . $MCF;


In reply to How does file uploading work in multipart form? by nysus

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.