http://www.perlmonks.org?node_id=992652

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

After spending all day messing around with a "click and initiate file download" javascript plugin and finally getting it to (to the best of my recolection) work, it obviously uses an ajax method of some type to pass along information to another script (page) which I can see, using tests, is starting the second script correctly, but, I have tried reading the information passed to it by using

read(STDIN, $Header, $ENV{'CONTENT_LENGTH'});

and

$Header = $ENV{'QUERY_STRING'};

and get no results. The only thing the author provides is a way to retrieve the data using PHP. Well, besides knowing next to nothing about PHP, and not even sure if my server is setup to read PHP, I'm stuck. I have successfully used a standard form to upload a file, so I'm not a greenhorn. Can anyone give me the perl conversion of the folowing PHP script so I can use Perl to proccess the data? ANY help would be appreciated since PHP is a little out of my scope *heh*

<?php
$tmp_file_name = $_FILES['Filedata']['tmp_name'];
$ok = move_uploaded_file($tmp_file_name, '/path_to/new_filename');
echo $ok ? "OK" : "FAIL";

I did try re-inventing the wheel...
But it kept getting stuck on the corners

Replies are listed 'Best First'.
Re: PHP to Perl ?
by GrandFather (Saint) on Sep 10, 2012 at 03:51 UTC

    That is a fragment of code that makes no sense in isolation, or at least, can't usefully be run in isolation. Here is a version with very minor changes to make the code look Perlish:

    my $tmp_file_name = $_FILES{Filedata}{tmp_name}; my $ok = move_uploaded_file($tmp_file_name, '/path_to/new_filename'); print $ok ? "OK" : "FAIL";

    which really doesn't get you any much further ahead I'm guessing because you don't know how the global "hash" %_FILES is populated (in PHP parlance it's an array, but PHP is a very confused language). You don't show the code for the sub move_uploaded_file and that is likely a less trivial conversion.

    Added: Oh, and most hosts that provide Perl will also provide PHP. It's unusual not to have PHP available on a web server.

    True laziness is hard work

      Actually, the snippet makes a lot of sense when you know it's PHP. The $_FILES superglobal is created when a form element to a submitted PHP page contains a file upload. The translation of:

      $tmp_file_name = $_FILES['Filedata']['tmp_name'];

      is "Retrieve the temporary file name PHP created (the tmp_name) of the form element Filedata and put it in the string tmp_file_name

      The next line:

      $ok = move_uploaded_file($tmp_file_name, '/path_to/new_filename');

      makes use of the PHP move_uploaded_file function (hence the OP can't show the source - you're asking for PHP's own source). That function takes the uploaded file and moves it to a directory specified by the second parameter.

      The Perl way to do this is best outlined (as Anonymous Monk already posted) in CGI - Processing a File Upload Field. The field name is Filedata, and from that you should be able to determine the rest.

        Actually... thank you for this, it helps a because I have not dealt with php, and it helps me to understand more of what the php script is expecting. I'm going to spend alot of time studying the article you mentioned that anonymous monk has provided. At first look, I'm having trouble figuring out how I'm going to incorporate the $_FILES['Filedata']['tmp_name'] into anonymous monk's $lightweight_fh  = $q->upload('field_name'), but I understand most of the rest of it. I guess, *heh*, wish me luck, php is not my forte'.

        So there's a harder way?
        I need to try that !
Re: PHP to Perl ?
by Anonymous Monk on Sep 10, 2012 at 03:54 UTC