Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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 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

Replies are listed 'Best First'.
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 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 You should be. Whether you're new, or a seasoned pro with Perl, there is almost no good reason to not use, 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.


      As I have mentioned in my post, I'm using the tutorial given at for the Perl script. So I guess I'm using the 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).


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.

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

        Really? I'm finding that hard to believe.

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 :


    #!/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;


    #!/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:

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://910133]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2017-12-17 04:56 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (462 votes). Check out past polls.