Specifications

LISTING 16.5 progex.phpFile Status Functions and Their Results
<?
echo “<pre>”;
// exec version
exec(“ls -la”, $result);
foreach ($result as $line)
echo “$line\n”;
echo “<br><hr><br>”;
// passthru version
passthru(“ls -la”);
echo “<br><hr><br>”;
// system version
$result = system(“ls -la”);
echo “<br><hr><br>”;
//backticks version
$result = `ls -al`;
echo $result;
echo “</pre>”;
?>
We could have used one of these approaches as an alternative to the directory-browsing script
we wrote earlier.
If you plan to include user-submitted data as part of the command youre going to execute, you
should always run it through the escapeshellcmd() function first. This stops users from mali-
ciously (or otherwise) executing commands on your system. You can call it like this, for example,
System(escapeshellcmd($command_with_user_data));
Interacting with the Environment: getenv() and
putenv()
Before we leave this section, well look at how you can use environment variables from
within PHP. There are two functions for this purpose:
getenv(), which enables you to retrieve
environment variables, and putenv(), which enables you to set environment variables.
Interacting with the File System and the Server
C
HAPTER 16
16
INTERACTING WITH
THE
F
ILE SYSTEM
AND THE
SERVER
367
21 7842 CH16 3/6/01 3:40 PM Page 367