Opening Pipes to and from Processes Using popen()


Opening Pipes to and from Processes Using popen()

Earlier in this chapter, you learned how to open a file for reading and writing using the fopen() function. Now, you'll see that you can open a pipe to a process using the popen() function.

The popen() function is used like this:

$file_pointer = popen("some command", mode)


The mode is either r (read) or w (write).

Listing 13.14 is designed to produce an errorit attempts to open a file for reading, which does not exist.

Listing 13.14. Using popen() to Read a File

 1: <?php 2: $file_handle = popen("/path/to/fakefile 2>&1", "r"); 3: $read = fread($file_handle, 2096); 4: echo $read; 5: pclose($file_handle); 6: ?>

We first call the popen() function in line 2, attempting to open a file for reading. In line 3, any error message stored in the $file_handle pointer is read and printed to the screen in line 4. Finally, line 5 closes the file pointer opened in line 2.

If you save this code as test_popen.php, place it in your document root, and access it with your web browser, you will see an error message such as this one:

The system cannot find the path specified.


Listing 13.15 also uses popen() to read the output of a process, in this case the output of the UNIX who command.

Listing 13.15. Using popen() to Read the Output of the UNIX who Command (UNIX Only)

 1:  <?php  2:  $handle = popen("who", "r");  3:  while (!feof($handle)) {  4:    $line = fgets($handle,1024);  5:    if (strlen($line) >= 1) {  6:        echo $line."<br/>";  7:    }  8:   }  9:   pclose($handle); 10:  ?>

In line 2, a file pointer is returned when we use popen() for reading. Line 3 begins a while loop, which reads each line of output from the process and eventually prints the lineif it contains informationin line 6. The connection is closed in line 9.

If you save this code as popen_who.php, place it in your document root, and access it with your web browser, you may see something like the following (with your actual information, not mine, of course):

julie pts/0 Mar 14 06:19 (adsl-63-206-120-158.dsl.snfc21.pacbell.net)


Listing 13.16 shows how to use popen() in write mode to pass data to an external application. The external application in this case is called column. The goal of the script is to take the elements of a multidimensional array and output them in table format, in an ASCII file.

Listing 13.16. Using popen() to Pass Data to the UNIX column Command (UNIX Only)

 1: <?php  2: $products = array(  3:             array("HAL 2000", 2, "red"),  4:             array("Tricorder", 3, "blue"),  5:             array("ORAC AI", 1, "pink"),  6:             array("Sonic Screwdriver", 1, "orange")  7:            );  8:  9: $handle = popen("column -tc 3 -s / > /somepath/purchases.txt", "w"); 10: foreach ($products as $p) { 11:    fputs($handle, join('/',$p)."\n"); 12: } 13: pclose($handle); 14: echo "done"; 15: ?>

In lines 27, we define a multidimensional array called $products and in it place four entries representing products with names, quantities, and colors. In line 9, popen() is used in write format to send a command to the column application. This command sends parameters to the column application telling it to format the input as a three-column table, using / as a field delimiter. The output will be sent to a file called purchases.txt (be sure to change the pathname to one that exists on your system).

In lines 1012, we use foreach to loop through the $products array and send each element to the open file pointer. The join() function is then used to convert the arrays to a string, with the specified delimiter appended to it. We close the open file pointer in line 13, and in line 14 print a status message to the screen.

If you save this code as popen_column.php, place it in your document root, and access it with your web browser, it should create a file in the specified location. Looking at the file created on my machine, I see the following text:

HAL 2000           2 red Tricorder          3 blue ORAC AI            1 pink Sonic Screwdriver  1 orange


You may or may not have the column program on your system, but this section illustrated the logic and syntax for opening a pipe to an application. Feel free to try out this logic with other programs available to you.




Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net