If you're on Windows, try using:
$WshShell = new COM("WScript.Shell");
$WshShell->Run('%COMSPEC% /C '.$file, 0, false);
If you do not want to trust at %COMSPEC%
specify which " interpreter " you want, usually %WINDIR%\system32\cmd.exe
is the default and it will be used.
Test this:
my.bat
@ECHO off
TIMEOUT /T 5 /NOBREAK
START %WINDIR%\system32\calc.exe
This will wait 5 seconds and then open the calculator ( %WINDIR%\system32\calc.exe
).
my.php
<?php
$inicio = time();
$arquivo = getcwd().DIRECTORY_SEPARATOR.'meu.bat';
$WshShell = new COM("WScript.Shell");
$WshShell->Run('%COMSPEC% /C '.$arquivo, 0, false);
$fim = time();
echo 'Finalizado em '.($fim - $inicio).' segundos';
Outcome:
PHP will return the text "Finished in 0 seconds", meanwhile the CMD will open the calculator after 5 seconds.
The PHP return is before the end of the CMD run. ;)
Another observation is that if PHP is running "on the same client machine" ( as in localhost ), no CMD window will be shown to the user, thus allowing to execute any command silently .
You need to enable the php_com_dotnet.dll
extension in PHP.ini!