If you’re on Windows, try using:
$WshShell = new COM("WScript.Shell");
$WshShell->Run('%COMSPEC% /C '.$file, 0, false);
If you don’t want to entrust in the %COMSPEC%
specify which "interpreter" wishes, usually the %WINDIR%\system32\cmd.exe
is the standard and it will be used.
Test this:
my bat.
@ECHO off
TIMEOUT /T 5 /NOBREAK
START %WINDIR%\system32\calc.exe
This will make you 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';
Upshot:
PHP will return the "Finished in 0 seconds" text, while CMD will open the calculator after 5 seconds.
The return of PHP is before the end of CMD execution. ;)
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.
It is necessary to enable the extension php_com_dotnet.dll
in PHP.ini!
Create another bat that has the function of just starting the slow bat. Example:
start myBatch.bat
.– Laércio Lopes
@Laérciolopes I created here a bat to start the main bat, and yet PHP stays locked until the bat is finished running.
– Leo Letto