Opening Pipes to and from Processes Using popen()In Chapter 12, "Working with Files and Directories," 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.1 is designed to produce an errorit will attempt to open a file for reading, which does not exist. Listing 13.1. Using popen() to Read a File1: <?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 utilizes 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 the following error message displayed on your screen: sh: /path/to/fakefile: No such file or directory Listing 13.2. Using popen() to Read the Output of the Unix who Command1: <?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 will read each line of output from the process and eventually print 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 Aug 27 04:59 (adsl-63-206-120-158.dsl.snfc21.pacbell.net) Listing 13.3 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.3. Using popen() to Pass Data to the Unix column Command1: <?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 in 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 it out with other programs available to you. |