将Perl-output返回给PHP

我想将perl脚本的输出返回到网页.但是它只返回最后一行.

Perl脚本:

my $directory = $ARGV[0];
opendir(DIR,$directory);
my @files = grep {/\.txt$/ } readdir(DIR);
closedir(DIR);
foreach(@files) {
    print $_."\n";
}

PHP代码

$perl_result = exec("perl $script_folder $project_folder");
*some code*
<?PHP print $perl_result; ?>

预期输出(以及脚本在Linux命令行中返回的内容):

test.txt
test2.txt
test3.txt

PHP返回的内容

test3.txt

我需要在代码中更改以使PHP显示所有行?

谢谢

解决方法:

引自PHP manual page for exec()

Return Values

The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the 07001 function.

To get the output of the executed command, be sure to set and use the output parameter.

所以一个建议就是停止使用exec()并开始使用passthru().然而,那是胡说八道. passthru()实际上并没有返回任何东西.如果你需要用$perl_result将它打印到浏览器就足够了,因此根本不需要将输出存储在变量中.但是如果你需要匹配输出,或以任何方式操纵它,你不需要passthru().

相反,尝试backtick operator

<?PHP
$perl_result = `perl $script_folder $project_folder`;

或者尝试将exec()的第二个参数设置为空数组:

<?PHP
$perl_result = array();
exec("perl $script_folder $project_folder", $perl_result);

$perl_result = implode("\n", $perl_result);  # array --> string

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...