HomePHP Page 2 - PHP File and Operating System Program Execution Functions
More PHP Program Execution Functions - PHP
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).
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:
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.