Jump to content

shell_exec


sabyasachi

Recommended Posts

$_SERVER['DOCUMENT_ROOT'].'/../cgi-bin/myexe' cannot exist... your combining a static url, with a relative... the ../ is whats causing that issue

 

I'm not sure about windows, but on *nix systems you can do that,  i.e.,

 

% cd /etc/../etc

 

moves my cwd to /etc.

 

It is very important to remember that when running shell_exec you will only get a return value if the program you ran outputs to stdout.  A problem alot of folks run into is when an error occurs in the program that output is sent to stderr.  You need to explicitly listen to stderr when running external programs.  I use this approach:

 

<?php
/**
* Define the pipes for our program
*/
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin (we're not going to use this) 
   1 => array("pipe", "w"),  // stdout (this is what program would send to the screen) 
   2 => array("pipe", "w") // stderr (this is what the program sends to the screen on error)
);

/**
* Define an array of pipes.
*/
$pipes = array();

/**
* Will use date as an example... 
* you can replace with whatever you would pass to shell_exec
*/
$process = proc_open('date', $descriptorspec, $pipes);

$stdin = $pipes[0];
$stdout = $pipes[1];
$stderr = $pipes[2];

/**
* First, let's check stderr
*/ 
if(($errOut = fread($stderr, 4096)) != ''){
echo 'Error: ' . $errOut;
exit(1);
}
/**
* Cool, no error... now we can see what the program output
*/
$out = fread($stdout, 4096);
echo $out;

/**
* Close our process and our pipes;
*/
proc_close($process);
fclose($stdin);
fclose($stdout);
fclose($stderr);

?> 

 

Hope this helps.

 

Patrick

Link to comment
https://forums.phpfreaks.com/topic/52952-shell_exec/#findComment-261694
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.