微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐