reopen a pipe in php for writing new commands to an executable

91 views Asked by At

I'm trying to send multiple commands (separated) to a program using php, I'm using /bin/bash for my test now and I'm facing a problem when I try to reopen the pipe to write the new command. here is the code , the commented part is the part where I tried reopening the pipe but with no luck it was commented because if I uncomment it the execution fails and I get a blank page.

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);



$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("file", "/tmp/error-output.txt", "a")
);
$cwd = '/tmp';
$env = array();

$process = proc_open('/bin/bash', $descriptorspec, $pipes, $cwd, $env);
$cmds = array("whoami", "pwd");
#for($i = 0; $i < 2; $i++){
if (is_resource($process)) {
        fwrite($pipes[0], $cmds[0]);
        fclose($pipes[0]);

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

}

/*
        popen($pipes[0], 'w');
        fwrite($pipes[0], $cmds[1]);
        fclose($pipes[0]);

        popen($pipes[1], 'r')
        echo stream_get_contents($pipes[1]);
        fclose($pipes[1]);
 */
proc_close($process);

0

There are 0 answers