Hello! Tell me how to run a binary with php? And anyway, is there any interaction between php and cpp?

  • system () not? or exec ... - johniek_comp

1 answer 1

proc_open - Executes the command and opens a pointer to the file for input / output (The proc_open function proc_open specific only for Unix systems) resource proc_open ( string $cmd , array $descriptorspec , array &$pipes [, string $cwd [, array $env [, array $other_options ]]] ) .

 <?php $descriptorspec = array( 0 => array("pipe", "r"), // stdin - канал, из которого дочерний процесс будет читать 1 => array("pipe", "w"), // stdout - канал, в который дочерний процесс будет записывать 2 => array("file", "/tmp/ error-output.txt ", "a") // stderr - файл для записи ); $cwd = '/tmp'; $env = array('some_option' => 'aeiou'); $process = proc_open('php', $descriptorspec, $pipes, $cwd, $env); if (is_resource($process)) { // $pipes теперь выглядит так: // 0 => записывающий обработчик, подключенный к дочернему stdin // 1 => читающий обработчик, подключенный к дочернему stdout // Вывод сообщений об ошибках будет добавляться в /tmp/ error-output.txt fwrite($pipes[0], '<?php print_r($_ENV); ?>'); fclose($pipes[0]); echo stream_get_contents($pipes[1]); fclose($pipes[1]); // Важно закрывать все каналы перед вызовом // proc_close во избежание мертвой блокировки $return_value = proc_close($process); echo "команда вернула $return_value\n"; } ?> 


 Array ( [some_option] => aeiou [PWD] => /tmp [SHLVL] => 1 [_] => /usr/local/bin/php ) команда вернула 0 

system - Runs an external program and displays its output. string system (string $ command [, int & $ return_var]) example

 <?php echo '<pre>'; // Выводит весь результат шелл-команды "ls", и возвращает // последнюю строку вывода в переменной $last_line. Сохраняет код возврата // шелл-команды в $retval. $last_line = system('ls', $retval); // Выводим дополнительную информацию echo ' </pre> <hr />Последняя строка вывода: ' . $last_line . ' <hr />Код возврата: ' . $retval; ?> 
  • Hmm, so I'm trying to run the file "bin" for example, and did not understand where to screw it? system ('bin', $ retval); system ('./ bin', $ retval); Doesn't work, or did I screw it in the wrong place? - karr
  • The server has its own environment (see the PATH variable), and its current directory. Perhaps system () simply does not find the file, you can try to run it with a full path. Then, the launch may be banally banned (see safe_mode, safe_mode_exec_dir, etc.) - user6550
  • When never would have thought that to cause a binary in php such a problem. So in php.ini I changed the line safe_mode_exec_dir = "/ home / karr / php_bin", I didn’t understand whether quotes were needed or not. Then everything that can and cannot be given 777 rights, probably the only thing left is the root of recursively 777 rights. I run it like $ last_line = system ('/ home / karr / php_bin / test', $ retval); Or even how to run the binary correctly? I launch it in the console - "./name" and here I am xs ... I started it from the folder with my hands - the binary works. What to do in the end I do not know. - karr
  • Yes, it did not fit all. How to deal with the parameters safe_mode = Off, safe_mode_gid = Off, safe_mode_include_dir =, and how to specify paths with or without quotes in general? oh, this web, I remember somehow the Apache was picking, there was something similar, they didn’t put quotes where they couldn’t stand in any logical way, he didn’t start. Well, this is another story, as they say. - karr
  • The rights to execute the binary set? chmod + x test.bin Directory permissions are enough 755> How should I set my file permissions on cgi-scripts? > Executed via web by anyone: chmod 755 - vv2cc