2025年1月17日 星期五

PHP proc_open 摘要

 <?php
  $cmd = "sqlite3 1.db ";
  $interactive_cmd = "insert into a (a1,a2) values('中文','許');";
  $descriptorspec = array(
    0 => array("pipe", "r"),  // stdin
    1 => array("pipe", "w"),  // stdout
    2 => array("pipe", "w")   // stderr ?? instead of a file
  );

  $stdout = "";
  $stderr = "";

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

  if (is_resource($process)) {
    fwrite($pipes[0], $interactive_cmd . PHP_EOL);
    fclose($pipes[0]);

    while($s= fgets($pipes[1], 1024)) $stdout .= $s;  // read from the pipe
    fclose($pipes[1]);

    // stderr optional:
    while($s= fgets($pipes[2], 1024)) $stderr .= $s;
    fclose($pipes[2]);
  }

  echo "stdout: $stdout \nstderr: $stderr\n";
?>


沒有留言: