Running Commands with exec()

exec() is one of many functions that enable you to pass commands to the shell. The function requires a string representing the path to the command you want to run. It also optionally accepts an array variable that is populated with the command’s output and a scalar variable that is populated with the command’s return value.

To get a listing for the current working directory, for example, you might pass exec() the command "ls -al .". We do this in Listing 21.4 (line 7), printing the result to the browser.

Listing 21.4. Using exec() to Produce a Directory Listing
 1: <!DOCTYPE html PUBLIC
 2:   "-//W3C//DTD XHTML 1.0 Strict//EN"
 3:   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 4: <html>
 5: <head>
 6: <title>Listing ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.