繁体   English   中英

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

[英]Passing uploaded files from php to Perl script

我有一个perl脚本,它将输入作为文件并在文本文件中返回结果。 我想使用文件作为输入,由用户通过php页面上传。 为此,我该怎么办? 我有PHP 5.3.14和ActivePerl5.14.x。

在PHP中,上传文件后,首先将其放置在一个临时位置。 您可以使用move_uploaded_file()将其移动到另一个位置:

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

然后,您可以通过以下几种方式调用Perl脚本:

如何从PHP调用Perl脚本?

因此,假设您使用的是使用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
}

请注意,这假设Perl脚本将文件名作为参数。 可能是它从标准输入中读取了文件,但问题尚不清楚。 显然,如果是后者,则将有所不同,同样取决于调用Perl的方法。

谢谢@leftclickben

我已经解决了问题。 我使用了一种形式来从用户获取文件,然后将文件名保存到参数$file

然后我将$file传递给使用的Perl脚本

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

$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