Skip navigation

The passhtru command in php permits to get the outcome of a unix shell command. If you never knew or heard about it, take a look at it in the manual. It is a fantastic mean to use any shell, perl o unix command from within the php language itself as this allows to output the result of a command in a close fashion to what system() or exec() also do.
In my particular case I ran into a situation hard to debug in which a script was failing only when requested inside another, with either a require or include call. The script failed with a fatal error of memory exhaustion. I didn’t have much time to study and re-code the script to avoid the problem, but it may be worth mentioning the alternative use that helped me to circumvent the failure when I simply used passthru to grab the output of the php script and then print it out inside the function in the way I wanted.

Here’s what I pretty much did:

ob_start();
passthru("php  tree.php --section ".$section." | sed -e '1,8 d'", $notOk);
$tree_outcome = ob_get_contents();
ob_end_clean();

if($notOk==0){
 echo '<div id="siteTree">',$tree_outcome,'</div>';
}

See also that the output is being piped to a sed command only to filter out the first 8 lines that I didn’t use.
As an extra note I had to slightly modify the tree.php script to process the shell argument “section” being passed which now needed to be read inside the $_SERVER[‘argv’] variable.

I don’t know of any drawback or potential problems of this method, it’s a workaround that I was happy to find, so comments from php coders will be very welcome.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: