Prev | Current Page 370 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

pl script, this time
using system():
CHAPTER 10 ?–  WORKING WITH T HE FILE A ND OPERATING SYSTEM 307
$outcome = system("languages.pl", $results);
echo $outcome
?>
Returning Binary Output
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. Its prototype follows:
void passthru(string command [, int return_var])
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:
header("ContentType:image/png");
passthru("giftopnm cover.gif | pnmtopng > cover.png");
?>
Executing a Shell Command with 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 sibling, commonly sharing a key with the tilde (~) on most U.S.
keyboards. An example follows:
$result = `date`;
printf("

The server timestamp is: %s", $result);
?>
This returns something similar to the following:
The server timestamp is: Sun Mar 3 15:32:14 EDT 2007
The backtick operator is operationally identical to the shell_exec() function,
introduced next.
308 CHAPTER 10 ?–  WORKING WITH THE FI LE AND OPERATING SYSTEM
An Alternative to Backticks
The shell_exec() function offers a syntactical alternative to backticks, executing a
shell command and returning the output.


Pages:
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382