45

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.

RichieHindle
  • 244,085
  • 44
  • 340
  • 385
undefined
  • 4,770
  • 8
  • 52
  • 88

7 Answers7

58

You might need to run it via cmd, eg:

system("cmd /c C:[path to file]");
RichieHindle
  • 244,085
  • 44
  • 340
  • 385
  • There is also some great documentation for the SYSTEM() as well: http://us3.php.net/system – Phill Pafford May 07 '09 at 17:25
  • 1
    how 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? – undefined May 08 '09 at 11:32
  • 1
    @Stephen: To read output from the process, you need to run it with popen. See http://uk3.php.net/popen – RichieHindle May 08 '09 at 12:05
  • Maybe 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: http://forums.devshed.com/php-development-5/php-execute-exe-file-server-802783.html for more information – Ole_S May 14 '15 at 10:35
  • @Ole_S: The link you posted is broken, could you explain how to do it? Thank you – Renaud Feb 04 '20 at 15:41
  • Sorry, don't know after such a long time. Maybe try a web search "windows 10 change owner service" or something similar. – Ole_S Feb 05 '20 at 15:53
12
<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\Program Files\VideoLAN\VLC\vlc.bat');
?>
sth
  • 200,334
  • 49
  • 262
  • 354
mysoogal
  • 121
  • 1
  • 2
9

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 :)

Migisha
  • 415
  • 4
  • 10
7
<?php
 pclose(popen("start /B test.bat", "r")); die();
?> 
testaa
  • 71
  • 1
  • 1
  • 1
    Care to comment your code? Why are you propossing this if there is another accepted answer long ago with several upvotes? – Yaroslav Oct 05 '12 at 06:19
2

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");

calmchess
  • 118
  • 8
0

This snippet is from working code.

You can trigger bat file not only from Windows GUI or Task Scheduler, but directly from PHP script when you need it. But in most cases it will have execution for 30-60 sec. depending from your PHP configuration. If job in BAT file is long and you don't want to freeze your PHP scripts, you need to fork BAT job as another process using php.exe and not be dependable from Apache.

This runs in background mode in Windows, seen as separate processes cmd.exe and php.exe from Task Manager not halting your Apache PHP scripts. The messages produced by your script may be stored and retrieved back via log files.

In my case in file_scanner.php I do some heavy calculations in loop for big array of files which may last for few hours with php function sleep() not to overload CPU.

The success poiner result from file $r which you can query via ajax if you want to know success or fauty start. In my case file_scanner.php writes log file with messages somefile.jpg - OK wich you can load to your UI with AJAX every few seconds to show progress.

PHP

/**
 * Runs bat file in background mode
 *
 */
 function run_scanner() {

    $c='start /b D:\Web\example.com\tasks\file_scanner.bat'; 
    $r=pclose(popen($c, 'r')); 
    return json_encode(array('result'=>$r));

 }

BAT

@echo Off
D:\PHP\php.exe D:\Web\example.com\tasks\file_scanner.php > D:\Web\example.com\tasks\file_scanner.log
exit
Alex Khimich
  • 518
  • 4
  • 8
0

For anyone who needs to run a program in the background "without PHP waiting for it to finish" do this:

 pclose(popen("start /B ".$cmd, "r")); 

where $cmd is the string command for the program that you need to run (e.g. $cmd can equal notepad.exe or node Path\to\server.js).

Source: https://www.php.net/manual/en/function.exec.php (see Arno van den Brink's note in the section titled "User Contributed Notes").

fcdt
  • 2,151
  • 5
  • 9
  • 24