['pipe', 'w'], //stdout 2 => ['pipe', 'w'], //stderr ]; // Generate `$strcmd` below to use for user notifications. $strcmd = array_reduce( array_merge( [escapeshellcmd($command[0])], array_map( 'escapeshellarg', array_slice($command, 1, sizeof($command)))), '__command2string__', ""); $process = proc_open( $command, // `proc_open` auto-escapes array command $descriptors, $pipes); if(!is_resource($process)) { throw new \RunTimeException(sprintf( "PHP was not able to spawn the process [%s]. " . "Check system resources or paths.", $strcmd )); } // Read streams: $stdout = stream_get_contents($pipes[1]); $stderr = stream_get_contents($pipes[2]); // Clean streams and close process resource. fclose($pipes[1]); fclose($pipes[2]); $exitcode = proc_close($process); if($exitcode !== 0) { throw new \RuntimeException(sprintf( "Command [%s] failed with exit code %d.\n" . "Stdout: %s\n\nStderr: %s", $strcmd, $exitcode, trim($stdout), trim($stderr) )); } return [$exitcode, $stdout, $stderr]; } ?>