简体   繁体   English

PHP中的shell_exec返回空字符串

[英]shell_exec in PHP returns empty string

shell_exec and exec are not returning any content. shell_execexec不返回任何内容。 I can't figure out what's wrong. 我不知道怎么了。

Here's some code: 这是一些代码:

echo 'test: ';
$output = shell_exec('whoami');
var_export($output, TRUE);
echo PHP_EOL . '<br>' . PHP_EOL;

And here's the source of the output 这是输出的来源

test 2: 
<br>

I do not have control over the host, but I believe they're running SuPHP. 我无法控制主机,但我相信它们正在运行SuPHP。 According to phpinfo , safe_mode is off. 根据phpinfo ,safe_mode关闭。 Running whoami from SSH outputs the expected value. 通过SSH运行whoami输出预期值。

I'm at a loss. 我很茫然。 Any idea how to debug this? 知道如何调试吗?

You're never printing the $output variable. 您永远不会打印$output变量。 The var_export() call returns the content of the variable when you call it with a true second parameter, it does not print it directly. 当您使用true第二个参数调用var_export()调用时, 将返回变量的内容,但不会直接打印该变量。

If you want the output from a shell command read back into PHP, you're probably going to need popen() . 如果要将shell命令的输出读回PHP,则可能需要popen() For example: 例如:

if( ($fp = popen("some shell command", "r")) ) {
    while( !feof($fp) ) {
        echo fread($fp, 1024);
        flush(); // input will be buffered
    }
    fclose($fp);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM