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.
-
1First, what have you tried? Second, are you asking how to use an uploaded file in PHP? Or how to pass it from a perl script directly to a php script that you want to return results? – Jon Feb 02 '13 at 10:04
2 Answers
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.

- 1
- 1

- 4,564
- 23
- 24
-
Or is a pre-made script that takes an input from a form itself, and the user modifies the form to fit and outputs the text to the browser normally. ^^ – Jon Feb 02 '13 at 10:15
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";

- 2,132
- 3
- 20
- 33

- 120
- 1
- 2
- 9