Multiple input with proc_open()

北战南征 提交于 2019-12-20 03:43:09

问题


I'm currently working on an online program. I'm writing a php script that executes a command in the command line with proc_open() (under Linux Ubuntu). This is my code so far:

<?php
$cmd = "./power";

$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("pipe", "w"),
);

$process = proc_open($cmd, $descriptorspec, $pipes);

if (is_resource($process)) {

    fwrite($pipes[0], "4");
    fwrite($pipes[0], "5");
    fclose($pipes[0]);

    while($pdf_content = fgets($pipes[1]))
    {
        echo $pdf_content . "<br>";
    }
    fclose($pipes[1]);

    $return_value = proc_close($process);
}
?>

power is a program that asks for input 2 times (it takes a base and an exponent and calculates base ^ exponent). It's written in Assembly. But when I run this script, I get wrong output. My output is "1" but I expect 4^5 as output.

When I Run a program that takes one input, it works (I've tested an easy program that increments the entered value by one).

I think I'm missing something regarding the fwrite command. Could anyone please help me?

Thanks in advance!


回答1:


You forgot to write a newline to the pipe, so your program will think that it got only 45 as input. Try this:

fwrite($pipes[0], "4");
fwrite($pipes[0], "\n");
fwrite($pipes[0], "5");
fclose($pipes[0]);

Or shorter:

fwrite($pipes[0], "4\n5");
fclose($pipes[0]);


来源:https://stackoverflow.com/questions/10880589/multiple-input-with-proc-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!