How do you run a .bat file from PHP?
Solution 1
You might need to run it via cmd
, eg:
system("cmd /c C:[path to file]");
Solution 2
<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\Program Files\VideoLAN\VLC\vlc.bat');
?>
Solution 3
When you use the exec()
function, it is as though you have a cmd
terminal open and are typing commands straight to it.
Use single quotes like this $str = exec('start /B Path\to\batch.bat');
The /B
means the bat will be executed in the background so the rest of the php will continue after running that line, as opposed to $str = exec('start /B /C command', $result);
where command
is executed and then result
is stored for later use.
PS: It works for both Windows and Linux.
More details are here http://www.php.net/manual/en/function.exec.php :)
Solution 4
<?php
pclose(popen("start /B test.bat", "r")); die();
?>
Solution 5
on my windows machine 8 machine running IIS 8 I can run the batch file just by putting the bats name and forgettig the path to it. Or by putting the bat in c:\windows\system32 don't ask me how it works but it does. LOL
$test=shell_exec("C:\windows\system32\cmd.exe /c $streamnumX.bat");
Related videos on Youtube
undefined
Developer working mainly with Flash AS2 and AS3, PHP, HTML etc.
Updated on October 09, 2020Comments
-
undefined over 3 years
Can anyone tell me how to execute a .bat file from a PHP script?
I have tried:
exec("C:\[path to file]"); system("C:\[path to file]");
Nothing is working. I've checked the PHP manuals and googled around but can't find a good answer. Anyone know where I'm going wrong?
I'm running Windows 2003 Server and have successfully manually run the .bat file and it does what I need it to; I just need to be able to launch it programatically.
-
bool3max over 8 yearsFor some reason just doing exec("[filename here].bat") works perfectly, it just executes the file. No need for "cmd /c [filename here]".
-
-
Phill Pafford almost 15 yearsThere is also some great documentation for the SYSTEM() as well: us3.php.net/system
-
undefined almost 15 yearshow can i return the results from running the .bat file to PHP so that I can retrieve a value i need - and is this just slicing a string?
-
RichieHindle almost 15 years@Stephen: To read output from the process, you need to run it with popen. See uk3.php.net/popen
-
Yaroslav over 11 yearsCare to comment your code? Why are you propossing this if there is another accepted answer long ago with several upvotes?
-
Ole_S almost 9 yearsMaybe interesting for somebody: on windows systems with locallay installed XAMP this attempt maybe ends up in a timeout and the batch never is executed. If you have started XAMP as service windows will use something like this as user: nt-autorit„t\system and you are logged in with a user account. Change the owner of the service to your local user account and it will work. See: forums.devshed.com/php-development-5/… for more information
-
Peter T. about 7 yearsWhen Du I need "start" to run a batch in cmd?
-
DoctorDroid Haiti almost 5 yearsgreat I was using <?php excec('c:\Windows\system32\cmd.exe /c Start path/batch.bat') ?> This one worked but the page kept loading. Your line of code worked for me like a charm. <?php exec('start /B Path\to\batch.bat') ?> I recommend this
-
Renaud about 4 years@Ole_S: The link you posted is broken, could you explain how to do it? Thank you
-
Ole_S about 4 yearsSorry, don't know after such a long time. Maybe try a web search "windows 10 change owner service" or something similar.
-
Philip about 3 yearsThanks. Don't necessarily need the C:\windows\system32\cmd.exe /c