Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中有与子流程等效的吗?_Php_Shell_Exec - Fatal编程技术网

在PHP中有与子流程等效的吗?

在PHP中有与子流程等效的吗?,php,shell,exec,Php,Shell,Exec,在Java和Python中,您可以使用ProcessBuilder或模块来轻松地使用未经转换的字符串启动流程,例如[“ls”,“某些未经转换的目录名”]——它们还为您提供了强大的工具,如从stdout、stderr读取数据。PHP是否有任何等效功能比仅使用exec()更智能、更有用?通过双向通信访问stdin、stdout和stderr,最接近的等效功能是 以下是文档中的示例: <?php $descriptorspec = array( 0 => array("pipe",

在Java和Python中,您可以使用
ProcessBuilder
或模块来轻松地使用未经转换的字符串启动流程,例如
[“ls”,“某些未经转换的目录名”]
——它们还为您提供了强大的工具,如从stdout、stderr读取数据。PHP是否有任何等效功能比仅使用exec()更智能、更有用?

通过双向通信访问stdin、
stdout
stderr
,最接近的等效功能是

以下是文档中的示例:

<?php
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

$cwd = '/tmp';
$env = array('some_option' => 'aeiou');

$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/error-output.txt

    fwrite($pipes[0], '<?php print_r($_ENV); ?>');
    fclose($pipes[0]);

    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
}
?>
这将把
/path/to/executable
的输出读入一个输出行数组

你还问了关于逃避争论的问题。您可以通过以下方式实现:


老实说,PHP不是用来运行进程的。真的吗?这就是我的想法,尽管在现代PHP中,人们说可以使用PHP作为Bash之类的脚本语言——“PHP也可以用来构建强大的命令行应用程序(就像Bash、Ruby、Python等)。许多PHP开发人员没有意识到这一点,错过了一个真正令人兴奋的功能。”-不确定是否对您有帮助。PHP是一种脚本语言。但用于制作web应用程序,在web上运行。它不像Python或Java那样在主机上运行得比PHP更深。不确定,但会对您有所帮助
<?php
$handle = popen('/path/to/executable', 'r');

$lines = [];
while (!feof($handle))
{
    $lines[] = fgets($handle);
}

pclose($handle);
$escapedArg = escapeshellarg($arg);