The following is a bare bones upload script:

WEB PAGE:

<HTML> <TITLE>Upload File</TITLE> <BODY BGCOLOR="#FFFFFF"> <form method="post" action="/cgi-bin/upload.pl" enctype="multipart/for +m-data"> <input type="file" name="graphic" size="35"> <input type="submit" value=" Upload "> </form> </BODY> </HTML>
PERL SCRIPT:
#!/usr/local/bin/perl ############################################ $base = "/usr/home/web67378/www/htdocs"; $folder = "$base/upload"; use CGI; $query = new CGI; print "Content-Type: text/html\n\n"; if ($filename = $query->param('graphic')) { if ($filename =~ /^([\w-]+\.(jpg|gif|png))$/) { $file_handle = $filename; $filename = $1; open(OUT, ">$folder/$filename") || &error('The file could not be written to the server.'); while (read($file_handle,$buffer,1024)) { print OUT $buffer; } close($file_handle); close(OUT); } } else { &error('No file was selected for upload.'); } print "Successful."; sub error { print shift; exit; }
Items of note:
1) Edit $base and $folder as necessary.

2) if ($filename =~ /^([\w-]+\.(jpg|gif|png))$/) {
Edit this so it matches your desired file name pattern. The current pattern is for image files only.

3. Add proper header and footer HTML so the output doesn't look like crud.


In reply to Re: Picture upload script! by TedPride
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.