2

this is my first post and I would like to apologise in advance in case it's not worded or structured in the best way.

I am using Windows7 Home Premium with IIS 7.5 and PHP 5.3 I am executing the following code in php and it doesn't work. The exec command returns 1 and an empty array.

$path = "\\\\somecomputer\\somepath\\afolder";
chdir($path);
$cmd = "pushd $path";
exec("echo off & $cmd & \"c:/bfolder/somexecutable.exe\" -flag1 -flag2 \"$inputfile\" > outputfile.log", $retary, $retval);
print_r($reary);
print $retval;

However, if I do not chdir to a network path prior the exec call then everything works fine. It seems that when php cwd is set to a Network path any exec initiated from then on fails.

To sum up, I need c:\afolder\win32\pdftotext.exe to run from PHP using exec and read it's input file from a network-share and write its output but on a Windows Network Location.

tommizi
  • 21
  • 2

1 Answers1

1

I am not sure I fully understand your question, but some things jump to mind that might help.

Does the account that iis is running under (app pool identity and/or authentication) have access to the network share? Try a real user rather than a system account.

Can you use a unc path directly in the exec call as a parameter (do you need to call chdir())?

Your print_r($reary) should be print_r($retary)

Adding ." 2>&1" to the end of the command executed on the shell, usually gets you some output back which can be useful for debugging. For example this is a method we use for executing on the windows command line:

    /**
* Execute the command - remember to provide path to the command if it is not in the system path
* 
* @param string $command The command to execute through the Windows command line
* @param string &$output This is any output returned from the command line
* @return int The return code 0 normally indicates success.
*/
static public function Execute($command, &$output)
{
     $out = array();
     $ret = -1;

     exec($command." 2>&1",$out,$ret);         

     foreach($out as $line)
     {
        $output.= $line;
     } 
     return $ret;            
}

used as follows:

$output = '';
if(WindowsUtiliy::Execute('shell command', $output) != 0)
{
    die($output);
}

Sorry if I missed the point!

Gavin
  • 2,153
  • 2
  • 25
  • 35
  • It seems that when the PHP cwd is pointed at a UNC path then any call to the exec() function, which if I am not mistaken starts a new process with "cmd.exe /C" under the same user as the php, fails. I initially thought it was my escaping characters when I was trying to make my commands robust for possible spaces in the paths and/or file names. It was only when I tried few simple commands like "%CD%" and "dir" in the exec() that I realised that something else was up. Currently I have my own wrapper around exec which changes to a local drive - chdir("c:\\"); - prior exec(), much like yours above – tommizi Sep 23 '12 at 20:26
  • That is of course only a work around but I didn't know what else to do. I believe the route of the problem lays with the way php is passing its cwd to cmd.exe /C and how the latter cannot change to it if it is a UNC path. This is only a speculation..but I imagine php does some initial cd $phpcwd when the cmd.exe process is started. cd and UNC paths don't mix, only pushd works which temporarily maps the unc path to a drive:\somepath\afolder – tommizi Sep 23 '12 at 20:35
  • All you have stated above I have tried, exec with the unc path hard coded into the command. If php cwd was on the unc it failed, if prior that exec changed to i.e. c:\ it worked. Bug or a feature only time will show :-) – tommizi Sep 23 '12 at 20:37