通过shell_exec意外输出


Unexpected output via shell_exec

我想写一个后台任务。所以我写了一个基本的脚本来查看shell_exec的工作情况。脚本如下:

<?php
$op = shell_exec("php -v");
echo $op;
echo "back to the test.php";
?>

输出应该是php版本信息,但它会打印以下意外输出:

back to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.phpback to the test.php

它打印了我当前页面的内容59-60次,有时使用content-type:text/html,我尝试使用另一个php文件输出相同的

然后我尝试了使用诸如date之类的linux命令,它工作得很好吗请帮我解决这个我搞不清的问题。出了什么问题?

我已经尝试了php的绝对路径,但没有区别,还使用了其他php函数,如system、exec等。

编辑注意:我已将exec函数更改为shell_exec,因为我将其放错了位置。下面显示的输出来自shell_eexec

更新我还没有找到问题的解决方案,但找到了问题的原因——脚本中没有任何问题——这是因为服务器。我已经写信给服务器的支持中心,但没有得到回应,所以我认为这个问题已经结束了

我认为exec和这类函数在php.ini中被禁用了

if(function_exists('shell_exec')) {
    echo "shell_exec is enabled";
} else {
    echo "shell_exec is disabled";
}

打开php.ini并导航到disable_functions 部分

如果shell_exec列在下面,请将其删除。

然后重新启动apache/php处理程序。

此外,如果启用了安全模式,此功能将不可用。您需要禁用它。

exec函数的语法为

string exec ( string $command [, array &$output [, int &$return_var ]] )

所以它是第二个参数$output,它将由命令的输出填充。不过,它将返回数组,而不是字符串版本。您需要解析该数据才能获得版本。

exec("/usr/bin/php -v", $out);
print_r($out);

此外,我建议在exec函数中使用php的完整路径。