简体   繁体   中英

Passing uploaded files from php to Perl script

I have a perl script which takes input as a file and return results in text files. I want to use file as input, which is uploaded by user through php page. For that what should I do? I have PHP 5.3.14 and ActivePerl 5.14.x.

In PHP, when a file is uploaded, it is first placed in a temporary location. You can move it to another location using move_uploaded_file() :

http://php.net/manual/en/function.move-uploaded-file.php

Then, you can call your Perl script in a variety of ways, which are outlined here:

How can I call a Perl script from PHP?

So, let's say you are using the low-level method of using the exec() function, and the file is uploaded in a file upload field with name "userfile", you might use something like this:

$perlCommand = // ... something, e.g. from config ...
$workingPath = // ... something, e.g. from config ...
$filename = $workingPath . $_FILES['userfile']['name']
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $filename)) {
    $output = array();
    $return = 0;
    exec($perlCommand . ' ' . $filename, $output, $return);
    // Do something with $output and / or $return values
}

Note that this assumes that the Perl script takes the name of the file as an argument. It might be that it reads the file from standard input, it wasn't clear from the question. Obviously if it is the latter then it will be a bit different, again depending on the method you use to call Perl.

Thanks @leftclickben

I've solved the problem. I used a form to get file from user and then saved the filename to an argument $file .

Then I passed $file to Perl script using

$result = shell_exec("path\to\perl.pl" $file);
echo $result;

$file passed to perl.pl as an array named $ARGV[0]

#!usr/bin/perl

$filename = $ARGV[0];
open(HDL, $filename) or die "file not available, restart program\n";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM