View Single Post
  #2 (permalink)  
Old 04-03-2008, 01:28 PM
dman_2007
Guest
 
Posts: n/a
Default

The functions i discussed in my previous post and backticks operators are good when you want to run an external program just to get the output produced by the external program and the input required by the command can be supplied by using comman line parameters. But if you want to run an external program interactively, then you need to use following functions :

1) proc_open function

proc_open function takes three compulsory parameters. First parameter is the name of the external command to run. Second is an array which contains specifications of file descriptors, that is how do you want to connect with the external command's stdin, stdout, and stderr. Index 0 has specification for stdin, 1 for stdout and 2 for stderr. You can also specify more file descriptors which will be passed to external command and can be used for communication. The two pipe types which you can use are pipe and file. Third variable is passed as a reference which is set to be an array of file pointers corresponding to the descriptors specified in the second parameter. Optionally, you can also provide cwd, env and other options. For example, after running the following code :

Code:
<?php

 $proc = proc_open('wc', 
                   array(
                    0 => array('pipe', 'r'),
                    1 => array('pipe', 'w'),
                    2 => array('file', 'temp/log.txt', 'a')),
                   $fp_array);
  
 $input = <<<EDT
This is first line.
This is second line.
EDT;
 
 fwrite($fp_array[0], $input);
 fclose($fp_array[0]);
  
 $output = stream_get_contents($fp_array[1]);
 
 fclose($fp_array[1]); 
 proc_close($proc);
 
 echo $output;               
  
?>
we will get following output :

Quote:
1 8 40
Notice how we provided input to the external command by writing data to the stdin pipe of the external command and retrived command output by reading data from the stdout pipe of the external command. Be careful to close down connecting pipe file pointers before closing the process with proc_close function, otherwise a deadlock will be created and php will hang.

2) popen function

popen function also enables you to execute external program but if you use this function to run an external command then communication with the external command is unidirectional. That is you either write to external command's stdin or read from external command's stdout. popen function accepts two parameters. First parameter is the name of the external command to run and second is the mode of communication. For example, the following code will display the list of files in curent directory :

Code:
<?php

 $fp = popen('ls -l', 'r');
 $output = stream_get_contents($fp);
 pclose($fp);
 
 echo $output;               
  
?>
Notice how we used the pipe file pointer returned by popen to read the command output. Once the output is read, we close it with pclose function.

Last edited by dman_2007; 04-04-2008 at 01:28 PM.
Reply With Quote