13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Program Execution Functions<br />

449<br />

Listing 19.7<br />

Continued<br />

// exec(‘dir’, $result);<br />

foreach ($result as $line)<br />

echo “$line\n”;<br />

echo ‘’;<br />

echo ‘’;<br />

///// passthru version<br />

echo ‘’;<br />

// unix<br />

passthru(‘ls -la’) ;<br />

// windows<br />

// passthru(‘dir’);<br />

echo ‘’;<br />

echo ‘’;<br />

///// system version<br />

echo ‘’;<br />

// unix<br />

$result = system(‘ls -la’);<br />

// windows<br />

// $result = system(‘dir’);<br />

echo ‘’;<br />

echo ‘’;<br />

/////backticks version<br />

echo ‘’;<br />

// unix<br />

$result = `ls -al`;<br />

// windows0<br />

// $result = `dir`;<br />

echo $result;<br />

echo ‘’;<br />

? ><br />

You could use one of these approaches as an alternative to the directory-browsing script<br />

you saw earlier. Note that one of the side effects of using external functions is amply<br />

demonstrated by this code:Your code is no longer portable.This script uses Unix comm<strong>and</strong>s,<br />

<strong>and</strong> the code will clearly not run on Windows.<br />

If you plan to include user-submitted data as part of the comm<strong>and</strong> you’re going to<br />

execute, you should always run it through the escapeshellcmd() function first.This

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!