sabyasachi Posted May 25, 2007 Share Posted May 25, 2007 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' ? Quote Link to comment https://forums.phpfreaks.com/topic/52952-shell_exec/ Share on other sites More sharing options...
taith Posted May 25, 2007 Share Posted May 25, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/52952-shell_exec/#findComment-261499 Share on other sites More sharing options...
utexas_pjm Posted May 25, 2007 Share Posted May 25, 2007 $_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 Quote Link to comment https://forums.phpfreaks.com/topic/52952-shell_exec/#findComment-261694 Share on other sites More sharing options...
sabyasachi Posted May 26, 2007 Author Share Posted May 26, 2007 Thanks. This is weekend and I can not try your code. I'll get back on Monday. BTW: I have tried >cd "Default User\Application Data\..\Application Data" on windows XP and it works. Quote Link to comment https://forums.phpfreaks.com/topic/52952-shell_exec/#findComment-261930 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.