Hello, I have found a perl script on the internet I wish to use for uploading files to a site, but I have been unsuccessfull in using it. I am very new to perl, so am not sure what's wrong with the code. Here's the perl code:
#!/usr/bin/perl -wT #use CGI; use CGI qw(warningsToBrowser fatalsToBrowser); #use CGI::Carp qw(warningsToBrowser fatalsToBrowser); $CGI::POST_MAX = 10000000; # Added as an example - limit POST to 10000 +k my $q = new CGI; # Create new query object my $filename = $q->param("pic"); my $upload_filehandle = $q->upload("pic"); # Corrected my $upload_dir="/pics"; $filename =~ s/.*[\/\\](.*)/$1/; print $q->header ("text/html" ); open UPLOADFILE, ">$upload_dir/$filename"; binmode UPLOADFILE; # to specify binary file operations on this fileha +ndle my $total_bytes = 0; while (my $bytes_read = read($upload_filehandle, $buffer, 1024)) { print UPLOADFILE $buffer; $total_bytes += $bytes_read; } close $upload_filehandle; close UPLOADFILE; print "<html>"; print "<head>"; print "<title>Thanks..!!!</title></head>"; # note closing head tag print "<body>"; print "<P>Thanks for uploading your photo!</P>"; print "<P>Your photo:</P>"; print "<img src=\"/pics/$filename\" border=\"0\">"; print "<p>Size of photo is $total_bytes bytes.</p>"; # Added line if d +esired. print "</body>"; print "</html>";
and here's the HTML code:
<FORM ACTION="http://192.168.1.50/cgi-bin/upload2.pl" METHOD="post" EN +CTYPE="multipart/form-data"> Photo to Upload: <INPUT TYPE="file" NAME="pic"> <br><br> <INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form"> </FORM>
The html page works ok, put the picture isn't actually uploaded, so it just shows a broken picture icon. The apache error_log states: print() on closed filehandle main::UPLOADFILE at /Library/WebServer/CGI-Executables/upload2.pl line 23. and also that the .jpg file doesn't exist. I am testing it on my mac (OS 10.2) but but hope to put it up on my site at nic.nac.wdyn.de. Any help is much appreciated! From Edwin

In reply to 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.