【问题标题】:Unexpected output via shell_exec通过 shell_exec 的意外输出
【发布时间】:2015-03-24 12:03:16
【问题描述】:

我想写一个后台任务。所以我写了一个基本的脚本来查看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_exec

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

【问题讨论】:

    标签: php exec background-process shell-exec


    【解决方案1】:

    我认为 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 handler。

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

    【讨论】:

    • 我得到 shell_exec 已启用
    • 先生,如果函数不存在,它应该给未定义函数的错误调用...
    • 我已经完成了,它并没有解决我的问题,但它肯定会帮助初学者进行故障排除。
    【解决方案2】:

    exec 函数的语法是

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

    所以它是第二个参数 $output 将由命令的输出填充。它仍然会返回数组而不是版本作为字符串。您需要解析该数据以获取版本。

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

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

    【讨论】:

    • 谢谢@kuldeep.kamboj,但它在相同的输出上不起作用,但以数组格式输出
    • 是的,正如我所说的,它将作为数组输出出现。您需要解析第一个数组索引(或检查数组时的其他索引)以获取版本。
    • 但我的帖子中的输出与上面相同,我只是以数组格式显示
    • 来自手册页 -> The output from the executed command or NULL if an error occurred or the command produces no output. 。因此,如果发生任何错误,您返回的将为空。您应该检查命令是否有效。我再次建议使用完整的 php 路径而不仅仅是 php。在 Linux Ubuntu 中,它主要位于 /usr/bin/php 但您仍然可以检查它的安装位置。
    • 我也给出了php位置的完整路径
    猜你喜欢
    • 2010-11-26
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    相关资源
    最近更新 更多