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

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

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

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

Julie C. Meloni

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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







Running Commands with exec()


The exec() function is one of several functions you can use to pass commands to the shell. The exec() function requires a string representing the path to the command you want to run, and optionally accepts an array variable which will contain the output of the command, and a scalar variable which will contain the return value (1 or 0). For example:



exec("/path/to/somecommand", $output_array, $return_val);

Listing 12.4 uses the exec() function to produce a directory listing with the shell-based ls command.

Listing 12.4 Using exec() and ls to Produce a Directory Listing


1: <?php
2: exec("ls -al .", $output_array, $return_val);
3: echo "Returned $return_val<br>";
4: foreach ($output_array as $o) {
5: echo "$o <br>";
6: }
7: ?>

In line 2, the exec() function issues the ls command, with the output of the command placed in the $output_array array and the return value placed in the $return_val variable. Line 3 simply prints the return value, while the foreach loop in lines 46 prints out each element in $output_array.

If you save this code as listing12.4.php, place it in your document root, and access it with your Web browser, you may see something like Figure 12.1 (with your actual information, not mine, of course).

Figure 12.1. Output of a script that uses exec() to list directory contents.


In the previous chapter, you learned to use opendir() and readdir() to display a directory listing, but this was a simple example of using exec() to perform a task using system tools. There will be times when running a command on your system will achieve an effect that may take a great deal of code to reproduce in PHP. For example, you may have already created a shell script or Perl script that performs a complex task in a short period of time; rather than reinvent the wheel using PHP, you can simply use exec() to access the existing script. However, remember that calling an external process will always add some amount of additional overhead to your script, in terms of both time and memory usage.


/ 323