Intereting Posts

Как выбрать пользовательский ввод из сценария оболочки в PHP?

У меня есть сценарий оболочки, запущенный с сайта PHP.

В сценарии оболочки (скрипт оболочки ревизии) у меня есть 3 варианта:

1) Сценарий процесса 2) Результаты отображения 3) Выход

Пробовал коды ниже и, похоже, не работает, на PHP-сайте отображаются пробелы.

<?php session_start(); exec('/Desktop/test.sh'); exec('1'); $output = exec('2'); echo "<pre>$output</pre>"; ?> 

Любая помощь будет оценена.

Related of "Как выбрать пользовательский ввод из сценария оболочки в PHP?"

 <?php session_start(); // This line executes '/Desktop/test.sh' as if it had been called from the // command line // exec('/Desktop/test.sh'); // This line attempts to execute a file called '1', which would have to be // in the same directory as this script // exec('1'); // This line attempts to execute a file called '2', which would have to be // in the same directory as this script, and capture the first line of the // output in $output // $output = exec('2'); // I think you want to be doing something more like this - this executes the // shell script, passing "1" and "2" as arguments, and captures the whole // output as an array in $output exec('/Desktop/test.sh "1" "2"', $output); // Loop the output array and echo it to the browser echo "<pre>"; foreach ($output as $lineno => $line) echo "Line $lineno: $line\n"; echo "</pre>"; ?> 

Мне кажется, что вы могли бы сделать с чтением страницы руководства для exec() правильно …

Попробуйте использовать proc_open вместо exec; это дает вам больший контроль над процессом ввода / вывода. Что-то вроде:

 <?php $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("file", "/dev/null", "a") // stderr is a file to write to ); $cwd = '/Desktop'; $env = array(); $process = proc_open('/Desktop/test.sh', $descriptorspec, $pipes, $cwd, $env); if (is_resource($process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be sent to /dev/null (ie, discarded) fwrite($pipes[0], "1\n"); fwrite($pipes[0], "2\n"); fclose($pipes[0]); $output = stream_get_contents($pipes[1]); fclose($pipes[1]); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $return_value = proc_close($process); echo $output; } ?> 

Примечание. Я снял этот код с страницы proc_open на PHP-руководстве