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

Hello, long-time lurker, first time poster here. I've been having an issue with my CGI script to upload files to a web server. I think that I may have a handle on what the issue is, but I'm not quite sure.

I don't have a great grasp of technical definitions, so I'll try to explain what's happening. I have a CGI script that prints an html form that takes in a few parameters, which recently includes a "file" input. It then passes the information to another CGI script to validate the information, which then gets passed to yet another to post everything. This is mostly legacy code, and input data comes from many different places. What I am running is mostly a slightly modified version of this excellent script: I need a simple web upload script. The major difference is that there is no email that is sent.

The file is getting created in the correct directory, but there is never any data passed along. I've been afraid that passing the file two times has somehow transferred the data from being a file to just the file title in a string. Does anyone have any thoughts? I can provide any code examples if people would like.

EDIT: The issue, I eventually learned, is that files need to be passed from a form with "post" rather than "get", as everything else in this package runs. Not a very exciting answer, but using "get" will not pass along any of the data.

Replies are listed 'Best First'.
Re: Issue with Passing Files?
by roboticus (Chancellor) on Jul 21, 2014 at 14:43 UTC

    Hermano23:

    Looking at the link you provided, it appears that the upload_file parameter is the one with the file data, so you may just need to pass that parameter along in addition to the filename. I don't know how your script is constructed, so that may be a bit oversimplistic, though.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I'll give it a shot, thank you for the quick reply.

Re: Issue with Passing Files?
by RonW (Parson) on Jul 21, 2014 at 19:52 UTC

    Looking at atcroft's script, $query->upload('upload_file') returns a file handle, which the script then reads, saving in a local file, based on the file name sent by the remote user's browser:

    my $fh = $query->upload('upload_file'); my $filename = $query->param('name'); $filename =~ s/[^A-Za-z0-9\.\_]/_/g; open OUTF, "> $directory$filename" or die; binmode OUTF; while ( $bytesread = read $fh, $buffer, 1024 ) { print OUTF $buffer; } close OUTF;

    So, the uploaded file should be in the directory specified by $directory.

    On the other hand, $query->param('upload_file') should contain the file content, probably in an encoded form (something like "x-html-form-encoded" or similar).