13.09.2016 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

448 Chapter 19 Interacting with the File System <strong>and</strong> the Server<br />

n<br />

n<br />

n<br />

The exec() function has no direct output. It returns the last line of the result of<br />

the comm<strong>and</strong>.<br />

If you pass in a variable as result, you will get back an array of strings representing<br />

each line of the output. If you pass in a variable as return_value, you will get<br />

the return code.<br />

passthru()—The passthru() function has the following prototype:<br />

void passthru (string comm<strong>and</strong> [, int return_value])<br />

The passthru() function directly echoes its output through to the browser. (This<br />

functionality is useful if the output is binary—for example, some kind of image<br />

data.) It returns nothing.<br />

The parameters work the same way as exec()’s parameters do.<br />

system()—The system() function has the following prototype:<br />

string system (string comm<strong>and</strong> [, int return_value])<br />

The function echoes the output of the comm<strong>and</strong> to the browser. It tries to flush<br />

the output after each line (assuming you are running <strong>PHP</strong> as a server module),<br />

which distinguishes it from passthru(). It returns the last line of the output<br />

(upon success) or false (upon failure).<br />

The parameters work the same way as in the other functions.<br />

Backticks—We mentioned backticks briefly in Chapter 1,“<strong>PHP</strong> Crash Course.”<br />

They are actually execution operators.<br />

They have no direct output.The result of executing the comm<strong>and</strong> is returned as a<br />

string, which can then be echoed or whatever you like.<br />

If you have more complicated needs, you can also use popen(), proc_open(), <strong>and</strong><br />

proc_close(), which fork external processes <strong>and</strong> pipe data to <strong>and</strong> from them.The last<br />

two of these functions were added at <strong>PHP</strong> 4.3.<br />

The script shown in Listing 19.5 illustrates how to use each of the four techniques in<br />

an equivalent fashion.<br />

Listing 19.7<br />

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

Saved successfully!

Ooh no, something went wrong!