php_mysql_apache [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

php_mysql_apache [Electronic resources] - نسخه متنی

Julie C. Meloni

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Opening Pipes to and from Processes Using popen()


In Chapter 11, "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 12.1 is designed to produce an errorit will attempt to open a file for reading, which does not exist.

Listing 12.1 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: ?>

Line 2 utilizes the popen() function, 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 listing12.1.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 12.2 Using popen() to Read the Output of the Unix who Command


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 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 listing12.1.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 Sep 30 21:06 (adsl-67-125-85-212.dsl.snfc21.pacbell.net)

Listing 12.3 shows how to use popen() in write mode, to pass data to an external application, in this case 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 12.3 Using popen() to Pass Data to the Unix column Command


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, a multidimensional array called $products is defined. In line 9, popen() is used in write format to send a command to the column application. The 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. Lines 1012 use foreach to loop through the $products array and send each element to the open file pointer. The join() function is used to convert the arrays to a string, with the delimiter appended to it. Line 13 closes the open file pointer, and line 14 prints a status to the screen.

If you save this code as listing12.3.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:



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


/ 323