tommizi
tommizi

Reputation: 21

PHP exec on windows - php cwd is a Windows Network Share

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.

Upvotes: 2

Views: 941

Answers (1)

Gavin
Gavin

Reputation: 2173

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!

Upvotes: 1

Related Questions