PHP File and Operating System Program Execution Functions

In this conclusion to an eight-part article series on working with a computer’s file and operating system with PHP, you’ll learn about the functions used in combination with a PHP script to execute system-level programs. This article is excerpted from chapter 10 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).

PHP’s Program Execution Functions

This section introduces several functions (in addition to the backticks execution operator) used to execute system-level programs via a PHP script. Although at first glance they all appear to be operationally identical, each offers its own syntactical nuances.

exec()

string exec (string command [, array output [, int return_var]])

The exec() function is best-suited for executing an operating system-level application (designated by command) intended to continue executing in the server background. Although the last line of output will be returned, chances are that you’d like to have all of the output returned for review; you can do this by including the optional parameter output, which will be populated with each line of output upon completion of the command specified by exec(). In addition, you can discover the executed command’s return status by including the optional parameter return_var.

Although we could take the easy way out and demonstrate how exec() can be used to execute an ls command (dir for the Windows folks), returning the directory listing, it’s more informative to offer a somewhat more practical example: how to call a Perl script from PHP. Consider the following Perl script (languages.pl):

#! /usr/bin/perl
my @languages = qw[perl php python java c]; foreach $language (@languages) {
print $language."<br />";
}

The Perl script is quite simple; no third-party modules are required, so you could test this example with little time investment. If you’re running Linux, chances are very good that you could run this example immediately, because Perl is installed on every respectable distribution. If you’re running Windows, check out ActiveStates (http://www.activestate.com/) ActivePerl distribution.

Like languages.pl, the PHP script shown here isn’t exactly rocket science; it simply calls the Perl script, specifying that the outcome be placed into an array named $results. The contents of $results are then output to the browser.

<?php
$outcome = exec("languages.pl", $results);
foreach ($results as $result) echo $result;
?>

The results are as follows:

——————————————–perl
php
python
java
c
——————————————–

system()

string system (string command [, int return_var])

The system() function is useful when you want to output the executed command’s results. Rather than return output via an optional parameter, as is the case with exec(), the output is returned directly to the caller. However, if you would like to review the execution status of the called program, you need to designate a variable using the optional parameter return_var.

For example, suppose you’d like to list all files located within a specific directory:

$mymp3s = system("ls -1 /home/jason/mp3s/");

Or, revising the previous PHP script to again call the languages.pl using system():

<?php
$outcome = exec("languages.pl", $results);
echo $outcome
?>

{mospagebreak title=More PHP Program Execution Functions}

passthru()

void passthru (string command [, int return_var])

The passthru() function is similar in function to exec(), except that it should be used if you’d like to return binary output to the caller. For example, suppose you want to convert GIF images to PNG before displaying them to the browser. You could use the Netpbm graphics package, available at http://netpbm.sourceforge.net/ under the GPL license:

<?php
header("ContentType:image/png");
passthru("giftopnm cover.gif | pnmtopng > cover.png");
?>

Backticks

Delimiting a string with backticks signals to PHP that the string should be executed as a shell command, returning any output. Note that backticks are not single quotes, but rather are a slanted cousin, commonly sharing a key with the tilde (~) on most American keyboards. An example follows:

<?php
$result = `date`;
echo "<p>The server timestamp is: $result</p>";
?>

This returns something similar to:

——————————————–The server timestamp is: Sun Jun 15 15:32:14 EDT 2003
——————————————–

The backtick operator is operationally identical to the shellexec() function, introduced next.

shell_exec()

string shell_exec (string command)

The shell_exec() function offers a syntactical alternative to backticks, executing a shell command and returning the output. Reconsidering the preceding example:

<?php
$result = shell_exec("date");
echo "<p>The server timestamp is: $result</p>";
?>

Summary

Although you can certainly go a very long way using solely PHP to build interesting and powerful Web applications, such capabilities are greatly expanded when functionality is integrated with the underlying platform and other technologies. As applied to this chapter, these technologies include the underlying operating and file systems. You’ll see this theme repeatedly throughout the remainder of this book, as PHPs ability to interface with a wide variety of technologies like LDAP, SOAP, and Web Services is introduced.

In the next chapter, you’ll examine two key aspects of any Web application: Web forms and navigational cues.

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan