Mike Mike - 2 months ago 14
PHP Question

php Why can't I run a batch file?

I have tried to run a batch file using PHP:

$script = "\\\MAFINFWWWPV02\D$\WebContent\\engsys.corp.ftr.com\BatchFiles\CopyFiles.bat";
exec($script,$ReturnArray,$ReturnValue);
//shell_exec($script);
//system('cmd /c $script');
//system($script,$ReturnValue);


None of these work! I've tried

var_dump($ReturnValue); echo "<br>";
var_dump($ReturnArray); echo "<br>";


to try and see what is going on, but I get what appears to be normal output like this:


int(1)

array(0) { }


But the files that I'm trying to copy with my bat file, which works fine when run manually, don't get copied!

Edit additional question

Do the
\
need to be escaped in the file address?

EDIT 2

Here's what I get from running
icacls
:


CopyFiles.bat NT AUTHORITY\IUSR:(I)(RX)

NT AUTHORITY\SYSTEM:(I)(F)

NT AUTHORITY\NETWORK:(I)(RX)

CORP\ibb601:(I)(F)

CORP\taw330:(I)(F)

CORP\mmm976:(I)(F)

BUILTIN\Administrators:(I)(F)

BUILTIN\Users:(I)(RX,W)



I have full control and everybody else has at least read/execute.

EDIT 3

I have narrowed it down. It's not that the commands are not working it's that permission is denied to the files. Which I don't understand since everyone has write and read/execute access to the entire folder.

EDIT 4

I am running the commands from above trying to get it to work. I am using a function (
var_export(my_exec("shell_exec($script)"));
) to print what the errors are to my screen. I keep getting something like this:


'\'shell_exec\' is not recognized as an internal or external command, operable program or batch file.


I get a different one for each
shell_exec
,
system
, and
exec
. It just keeps saying that the command is not recognized. This is being executed on a Windows Server 2012 R2 Standard 64-bit. Is there something that I'm doing wrong with the commands?

Function that I'm using to print errors (I found it on another post):

function my_exec($cmd, $input='')
{
$proc=proc_open($cmd, array(0=>array('pipe', 'r'), 1=>array('pipe', 'w'), 2=>array('pipe', 'w')), $pipes);
fwrite($pipes[0], $input);fclose($pipes[0]);
$stdout=stream_get_contents($pipes[1]);fclose($pipes[1]);
$stderr=stream_get_contents($pipes[2]);fclose($pipes[2]);
$rtn=proc_close($proc);
return array('stdout'=>$stdout,'stderr'=>$stderr,'return'=>$rtn);
}

Answer

I finally got it to work. Here's what I ended up with:

$script = chr(92) . chr(92) . 'MAFINFWWWPV02\D$\WebContent\engsys.corp.ftr.com\BatchFiles\CopyFiles.bat';
if (!file_exists($script))
{
    var_dump($script); echo " Script<br>";
    echo "Script doesn't exist!<br>";
    var_dump(!file_exists($script));
}
else
{
    system('cmd /c ' . $script); echo " <br>";
}

Sometimes I still get the Script doesn't exist! message, but that is usually when I'm connected to the #1 server and not the #2 server. The .nat looks like this:

echo off
SET source2="\\MAFINFWWWPV02\engsys.corp.ftr.com"

START /wait NET USE L: "\\MAFINFWWWPV01\engsys.corp.ftr.com" Password /user:UserName

xcopy %source2% L: /E /Y /Q

NET USE L: /DELETE