IT Community - Software Programming, Web Development and Technical Support

Executing external programs via the shell in PHP

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 ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 05-24-2007, 01:09 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Executing external programs via the shell in PHP

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 02:20 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0