Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Binary File Upload

by codejone (Initiate)
on Dec 05, 2007 at 21:39 UTC ( [id://655227]=perlquestion: print w/replies, xml ) Need Help??

codejone has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Im trying to script that will accept a file from the post of an multipart/form-data HTML form. This script just gives me a generic "Error 500", is there any thing wrong with my code that I've missed?
#!/usr/bin/perl use CGI; $query = new CGI; $filename = $query->param('photo'); #remove slashes $filename =~ s/.*[\/\\](.*)/$1/; $upload_filehandle = $query->upload('photo'); open UPLOADFILE, ">>/path/to/$filename" ; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE; print $query->header( ); print <<EOM; <HTML> <HEAD> <TITLE>Thanks!</TITLE> </HEAD> <BODY> <P>Thanks for uploading your file!</P> <P>Your photo:</P> <img src="/$uploaddir/$filename" border="0"> </BODY> </HTML> EOM
it recives the post from a HTML form similar to this:
<FORM ACTION="/cgi-bin/test.pl" METHOD="post" ENCTYPE="multipart/form-data"> Photo to Upload: <INPUT TYPE="file" NAME="photo"> <INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form"> </FORM>

Replies are listed 'Best First'.
Re: Binary File Upload
by samtregar (Abbot) on Dec 05, 2007 at 23:01 UTC
    You need to find the error log for your web server. A "500" error is your web server's way of telling you "check the error log, something's broken." When you find the error message post it here if you're having trouble understanding what it's telling you.

    -sam

Re: Binary File Upload
by moritz (Cardinal) on Dec 05, 2007 at 23:05 UTC
    is there any thing wrong with my code that I've missed?

    Yes, you're missing essential parts of any good perl script:

    use strict; use warnings;

    And if you want to know why there is an "error 500", look in your webserver's error.log. (And don't forget to set the executable bit on the script).

Re: Binary File Upload
by almut (Canon) on Dec 05, 2007 at 22:24 UTC
    ... is there any thing wrong with my code that I've missed?

    You're not checking if the open UPLOADFILE, ... succeeded.  Even if the "500" error you're getting should not be immediately related, this is always a good idea to do...

Re: Binary File Upload
by aquarium (Curate) on Dec 05, 2007 at 22:40 UTC
    the regex looks wrong...as special characters lose their specialness whenever in a character class
    and altogether i'm not sure the regex is doing what you intended.
    the hardest line to type correctly is: stty erase ^H
Re: Binary File Upload
by tachyon-II (Chaplain) on Dec 05, 2007 at 23:18 UTC
Re: Binary File Upload
by Anonymous Monk on Dec 05, 2007 at 23:14 UTC
    the regex on the $filname varible is to remove the slash-dots in the file name sent by some browsers. is there a better way to do this?
      print the $filename after the regex (e.g. into a temp file.) when running this script from the browser.
      if the script doesn't get even that far, then something else is wrong (e.g. file permissions OR module not in libpath etc.)....in which case log into the box as the web server user (if you can) and try to execute the cgi script from the command line..........i.e. go back to basics, if you've coded the cgi without first ascertaining that a basic perl cgi works through www
      the hardest line to type correctly is: stty erase ^H

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://655227]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (None)
    As of 2024-04-25 00:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found