Jump to content

shell_exec


sabyasachi

Recommended Posts

I was trying to execute a file like :-

<?php

$cmd=$_SERVER['DOCUMENT_ROOT'].'/../cgi-bin/myexe'

 

$output = shell_exec($cmd);

 

echo $output;

?>

 

But it is not able to execute.

 

Is it that only those files I can execute which are within the subtree of 'httpdocs' ?

Link to comment
Share on other sites

a) whats it telling you?

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

c) "myexe"... whats the extension?

 

Link to comment
Share on other sites

$_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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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