I have a strange situation with a very simple batch file which I want to run from PHP, and get the exit code of this.
If I run the batch script from cmd.exe
, echo %errorleve%
will return the correct exit code, in this case 12
. From PHP script, exit code is 0
.
my_batch.bat file
@ECHO OFF
if "1"=="1" (
if "1"=="1" (
echo quitting
exit /B 12
)
echo anything
)
my_test.php
<?php
exec('my_batch.bat',$result,$exitcode);
echo $result[0];
echo '<br />';
echo $exitcode;
Output from cmd.exe
D:\tools\xampp\htdocs\test>my_batch.bat
quitting
D:\tools\xampp\htdocs\test>echo %errorlevel%
12
D:\tools\xampp\htdocs\test>
Output oh php:
quitting
0
Thank you for your support
EDIT 1
verry strange, if I change the code with this version, all works OK
my_batch.bat file
@ECHO OFF
if "1"=="1" (
if "1"=="1" (
echo quitting
SET MYERROR=12
GOTO:END
)
echo anything
)
:END
echo finished ... ERRORLEVEL "%MYERROR%"
exit /b %MYERROR%
Have you seen the documentation at http://php.net/function.exec.php? It tells you that a call of exec
spawns a instance of cmd.exe
and this calls your batch script. Probably, this intermediate process won't pass the return code through?
A solution could be to use proc_open