Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Perl CGI File Upload Problem

by nikhgeo (Initiate)
on Jun 17, 2011 at 12:07 UTC ( #910133=perlquestion: print w/ replies, xml ) Need Help??
nikhgeo has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I'm a newbie to Perl. I want to write a script to upload a file to server. Unfortunately my server do not allow to use the POST method.

When I try to save the file on the server, an empty file with the same file name is created. I used the code give at http://www.sitepoint.com/uploading-files-cgi-perl-2/ for the script (Just used GET method instead of POST)

I guess the below while loop is not working because when I tried incrementing a count variable inside that loop, it isn't getting incremented:

while ( <$upload_filehandle> ) { print UPLOADFILE; }

Is using GET method the problem? Please advice

Comment on Perl CGI File Upload Problem
Download Code
Re: Perl CGI File Upload Problem
by moritz (Cardinal) on Jun 17, 2011 at 12:11 UTC
    Is using GET method the problem?

    Yes. I don't think GET and uploads mix at all.

    Please advice

    Don't use GET for file uploads. Use POST or PUT.

      I tried using PUT, but still its not working. I can't use POST because my server do not allow to use that.

        Really? I'm finding that hard to believe.

        -derby

        What makes you think that your server does not allow you to use POST?

Re: Perl CGI File Upload Problem
by davido (Archbishop) on Jun 17, 2011 at 16:12 UTC

    Is it your server that disallows POST, or is your CGI.pm configured to disallow uploads? If it's the server at issue, you're going to have to either find a different service provider or give up on uploads. GET uses the URI, which isn't going to work out.

    It actually doesn't look like you're using CGI.pm. You should be. Whether you're new, or a seasoned pro with Perl, there is almost no good reason to not use CGI.pm, or one of its siblings such as CGI::Simple. But whichever module you use, you may find it necessary to explicitly allow uploads. Of particular interest will be the $CGI::POST_MAX and $CGI::DISABLE_UPLOADS flags.


    Dave

      As I have mentioned in my post, I'm using the tutorial given at http://www.sitepoint.com/uploading-files-cgi-perl-2/ for the Perl script. So I guess I'm using the CGI.pm module

      "But whichever module you use, you may find it necessary to explicitly allow uploads"

      Not sure about how to do that, but there is nothing said about that in the tutorial

      When I use the POST method, its giving an error " the requested method POST is not allowed for the url .."

        That is an Apache message (not CGI, not Perl). It occurs when you specify an HTML document as the action of a form (as opposed to a CGI script, for example), or when the webserver isn't set to recognize your script as executable (thus it treats it as a document, and you can't POST to a document).

        Within the form, the "action" should be the url of the script, not of another HTML document. If you already have it set as such, there may be a misconfiguration in Apache where it recognizes the script as a document rather than an executable program.

        See the following link: Why do I keep getting "Method Not Allowed" for form POST requests? (From the Apache FAQ).


        Dave

Re: Perl CGI File Upload Problem
by nikhgeo (Initiate) on Jun 20, 2011 at 09:23 UTC
    I'm including the code, I've used :

    fileupload.cgi

    #!/usr/bin/perl print "Content-type: text/html\n\n"; print <<HTML; <html> <head> <title> File Upload Sample</title> </head> <body> <h1>Upload File</h2> <form action="fileprocess.cgi" method="post" enctype="multipart/fo +rm-data"> <input type="file" name="alertsfile" /> <input type="submit" value="Upload"> </form> </body> </html> HTML exit;

    fileprocess.cgi

    #!/usr/bin/perl-wT use strict; use CGI; use CGI::Carp qw (fatalsToBrowser); use File::Basename; $CGI::POST_MAX = 1024 * 5000; my $safe_filename_characters = "a-zA-Z0-9_.-"; my $upload_dir = "/home/netcool/alertfiles"; my $query = new CGI; my $filename = $query->param("alertsfile"); if ( !$filename ) { print $query->header ( ); print "There was a problem uploading your photo (try a smaller fil +e)."; exit; } my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); $filename = $name . $extension; $filename =~ tr/ /_/; $filename =~ s/[^$safe_filename_characters]//g; if ( $filename =~ /^([$safe_filename_characters]+)$/ ) { $filename = $1; } else { die "Filename contains invalid characters"; } my $upload_filehandle = $query->upload("alertsfile"); open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!"; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE; print $query->header ( ); print "Content-type: text/html\n\n"; print <<HTML; <html> <head> <title>Thanks</title> </head> <body> <p>Thanks for uploading your file!</p> <p>Your file:</p> <p><href src="/upload/$filename" alt="File" /></p> </body> </html> HTML

        Looks awfully familiar to: http://www.sitepoint.com/uploading-files-cgi-perl-2/

        That url is listed in the OP, in Perl CGI File Upload Problem

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-09-22 10:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (188 votes), past polls