Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Yes you can.

The html that you use to create the form that you need to use can be created by CGI.pm, e.g.

#!/usr/local/bin/perl -w # script name = upload_form.cgi use strict; use CGI; $query = new CGI; print $query->header; print $query->start_html; print $query->start_multipart_form(-action=>"image_writer.cgi"); #belo +w print $query->filefield(-name=>'file'); print $query->submit(-value=>'Upload File'); print $query->end_form; print $query->end_html; exit;

Note that you have to use the start_multipart_form or else your upload will fail.

The next bit comes straight out of the docs for CGI.pm. You might consider reading them and discover the difference between the upload and param methods.

#!/usr/bin/local/perl -w # name image_writer.cgi use CGI; $query = new CGI; $file = $query->param('file'); open IMAGE, ">/tmp/image"; while ($bytesread = read($file,$buffer,1024) ) { #treat as binary $total += $bytesread; #add the bytesread to a total if ($bytesread > 2000000) { # no files larger than 2Meg, pleas +e close IMAGE; $badboy = $ENV{'REMOTE_ADDR'}; $time = scalar localtime; unlink ("/tmp/image"); #delete the file that's too large and l +og the offense (below) die "$time: $badboy tried to upload a file that was too large" +; } print IMAGE $buffer; } exit;

That's about all there is to it. If you are using a Windows web-server, you'll have to modify where the file is saved, but it should work, otherwise. (there may be some minor problems with the code, but it's sound theoretically).

I'd advise against using strict in the script that actually takes the upload, as there are some versions of CGI.pm that have problems with uploads with strict on (seems to be a problem with symbolic references or some other nonsense).


In reply to RE: Upload images via browser ??? by tenatious
in thread Upload images via browser ??? by vesakk

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others avoiding work at the Monastery: (5)
    As of 2024-07-18 21:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.