This is a discussion on Executing external programs via the shell in PHP within the PHP Programming forums, part of the Web Development category; PHP provides a number of functions for executing external programs via the shell, namely shell_exec(), passthru(), exec(), popen(), all of ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| PHP provides a number of functions for executing external programs via the shell, namely shell_exec(), passthru(), exec(), popen(), all of which do essentially the same thing, but provide different "interfaces" to the external program. Each of these functions can only capture output written to the STDOUT pipe; each will spawn a child process within which the external program will be executed. Two further functions exist to execute external programs: proc_open() and pcntl_exec(). Their use and behaviour differs from the preceding functions, so we'll examine them separately. shell_exec() The shell_exec() command is actually an alias for the backtick operator. It allows you to execute an external program via the shell and have the results returned to you as a string. Whether you use the backtick operator or the shell_exec() command is up to you. For example #!/usr/local/bin/php <?php $result = shell_exec(file_tobe_executed.php); fwrite(STDOUT,$result); exit(0); ?> passthru() The passthru() function allows you to execute an external program and display its results directly: For example #!/usr/local/bin/php <?php passthru(file_tobe_executed.php); exit(0); ?> exec() The exec() function provides another variation on the theme. It returns the last line of output from the external program, but also (optionally) populates an array with the full output and makes the return code available. If I assume that most programs will return a single-line error message, if indeed there was one, exec() can be pretty handy. For example #!/usr/local/bin/php <?php passthru(file_tobe_executed.php); exit(0); ?> system() The system() function provides yet another variation on the theme -- something between passthru() and exec(). Like passthru(), it also outputs directly anything it receives from the external program, on the STDOUT stream. However, like exec(), it also returns the last line output, and makes the return code available. popen() The popen() function allows me to work with an external program as if I were working with a file. For further more details on shell execution commands please see PHP on the Command Line - Part 2 [PHP & MySQL Tutorials]
__________________ With, J. Jeyaseelan Everything Possible |
| Sponsored Links |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Shell Script Tips & Tricks : | a.deeban | Operating Systems | 18 | 09-07-2007 05:48 PM |
| Executing-ShellCommands | Murali | Database Support | 2 | 07-28-2007 03:25 AM |
| Shell variables | vigneshgets | Operating Systems | 1 | 07-19-2007 12:16 PM |
| Executing SQL Server Stored Procedures With PHP - Executing stored procedures | Jeyaseelansarc | PHP Programming | 1 | 07-19-2007 12:23 AM |
| Error while executing fla | nssukumar | Flash Actionscript Programming | 0 | 07-16-2007 07:37 AM |