简体   繁体   English

将上传的文件从php传递到Perl脚本

[英]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. 我有一个perl脚本,它将输入作为文件并在文本文件中返回结果。 I want to use file as input, which is uploaded by user through php page. 我想使用文件作为输入,由用户通过php页面上传。 For that what should I do? 为此,我该怎么办? I have PHP 5.3.14 and ActivePerl 5.14.x. 我有PHP 5.3.14和ActivePerl5.14.x。

In PHP, when a file is uploaded, it is first placed in a temporary location. 在PHP中,上传文件后,首先将其放置在一个临时位置。 You can move it to another location using move_uploaded_file() : 您可以使用move_uploaded_file()将其移动到另一个位置:

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

Then, you can call your Perl script in a variety of ways, which are outlined here: 然后,您可以通过以下几种方式调用Perl脚本:

How can I call a Perl script from PHP? 如何从PHP调用Perl脚本?

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: 因此,假设您使用的是使用exec()函数的低级方法,并且该文件是在名称为“ userfile”的文件上传字段中上传的,则可能会使用如下所示的内容:

$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. 请注意,这假设Perl脚本将文件名作为参数。 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. 显然,如果是后者,则将有所不同,同样取决于调用Perl的方法。

Thanks @leftclickben 谢谢@leftclickben

I've solved the problem. 我已经解决了问题。 I used a form to get file from user and then saved the filename to an argument $file . 我使用了一种形式来从用户获取文件,然后将文件名保存到参数$file

Then I passed $file to Perl script using 然后我将$file传递给使用的Perl脚本

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

$file passed to perl.pl as an array named $ARGV[0] $file作为名为$ARGV[0]的数组传递给perl.pl

#!usr/bin/perl

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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