Hudzilla.org - the homepage of Paul Hudson
Contents > Functions Wish List | Report Bug | About Me ]

4.12     Executing external programs: exec(), passthru(), and virtual()

This is NOT the latest copy of this book; click here for the latest version.

string exec ( string command [, array output [, int return_var]])

void passthru ( string command [, int return_var])

int virtual ( string filename)

Despite PHP being a powerful language with many extensions available to handle specialist libraries, you will likely find it helpful to be able to run external programs when necessary, particularly if you run Unix where the OS comes with many more built-in programs.

In PHP there are two important methods to execute programs, and these are exec() and passthru(). Both of these two take a minimum of one parameter, which is the name of the program you want to run, but the difference between them is that exec() runs the program then sends back the last line outputted from that program as its return value. The passthru() function, on the other hand, runs the program specified and prints out all the output that program generates. Calling exec() is usually preferred when the output of your program is irrelevant, whereas passthru() automatically prints your output. Here is both in action:

<?php
    
print exec("uptime");
    
passthru("who");
?>

Both of those commands are available on standard Unix boxes. Note that as uptime usually returns just one line, it does not matter whether print exec() or passthru() is used.

If you pass a second and third parameter to exec(), the output of the command will be put into parameter two as an array with one line per element, and the return value of the command will be put into parameter three. Similarly, if you pass a second parameter to passthru() it will be filled with the return value of the command.

For example:

<?php
    exec
("dir", $output, $return);
    echo
"Dir returned $return, and output:\n";
    
var_dump($output);
?>

That example should work fine on Windows as well as many versions of Unix.

Author's Note: If your server is a Unix box, try using passthru("fortune") to get a quick and easy random quote system for the bottom of your pages. Note that fortune may not be installed or available to your PHP scripts - contact your system administrator to find out.

There are other execution functions available, notably shell_exec() and system(), however they are largely irrelevant - shell_exec(), for example, works in exactly the same way as the backtick operator we looked at earlier.

Author's Note: Taking user input and passing it to one of these program execution functions is potentially fatal - users can easily bypass security and do nasty things with your server. If you really must use user data as input to your program calls, pass it through the special function escapeshellcmd() first - it takes your input, and returns it in a safe format that can be used.

So far we've looked at executing external programs using exec() and system(), but there is a third function that allows you to execute externally also, although it works quite differently from the other two. The virtual() function takes just one parameter, and, unusually, only works on Apache and SunONE web servers. Unlike exec() and system(), virtual() performs a virtual request to the web server for a file, almost as if your script were a client itself. This request is processed as per usual and its output is sent back to your script.

Using this method you can, for example, execute a Perl script from your PHP script, or, for real weirdness, execute another PHP script from your PHP script. The uses for virtual() may not seem apparent at first, simply because they are few and far between. However, if you do have a page on your site that requires special execution, you either have to use exec(), passing in the name of the program that handles the page as well as the page filename itself, you can use include(), but only if the script is PHP (not likely, as include()/require() are superior for this task), or you can use virtual().





<< 4.11 Pausing script execution: sleep() and usleep()   4.13 Connection-related functions: ignore_user_abort(), register_shutdown_function(), and connection_status() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Theo - 29 Aug 2008

To: A PHP User

thank you!!

A PHP User - 29 Aug 2008

Can u specify how a php program run by using system command?.Explain the command used for to execute a progrm.
I think you done a good work and it is so helpful to me.

number.five - 29 Aug 2008

the parameters used as return values must be passed by reference, therefore the functions prototype for exec and passthru are wrong.

A PHP User on Windows - 29 Aug 2008

Here is something that got me making up many bad words before I managed to get these functions going:

On Windows NT, running Apache as a service, you will have to go to the Service manager "Run->'services.msc'-><OK>", select the Apache service, choose properties, and somewhere there check the box that has a similar text to: "Allow service to interact with desktop".



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow