fork download
  1. <?php
  2. $descriptorspec = array(
  3. 0 => array("pipe", "r"), // stdin - канал, из которого дочерний процесс будет читать
  4. 1 => array("pipe", "w"), // stdout - канал, в который дочерний процесс будет записывать
  5. 2 => array("pipe", "w") // stderr - файл для записи
  6. );
  7.  
  8. $cwd = '/';
  9.  
  10. $env = array('some_option' => 'aeiou');
  11.  
  12. $process = proc_open('cat', $descriptorspec, $pipes, $cwd, $env);
  13.  
  14. if (is_resource($process)) {
  15. // $pipes теперь выглядит так:
  16. // 0 => записывающий обработчик, подключенный к дочернему stdin
  17. // 1 => читающий обработчик, подключенный к дочернему stdout
  18. // 2 => читающий обработчик, подключенный к дочернему stderr
  19.  
  20. fwrite($pipes[0], '5');
  21. fclose($pipes[0]);
  22.  
  23. echo stream_get_contents($pipes[1]);
  24. fclose($pipes[1]);
  25.  
  26. echo "\n-----------\nstderr: \n".stream_get_contents($pipes[2]);
  27. fclose($pipes[2]);
  28. // Важно закрывать все каналы перед вызовом
  29. // proc_close во избежание мертвой блокировки
  30. $return_value = proc_close($process);
  31.  
  32. echo "команда вернула $return_value\n";
  33. }
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
5
-----------
stderr: 
команда вернула 0