Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute batch file via PHP?

I tried to execute the batch file using exec command in PHP. I just used it like:

$filename = 'test.bat';
exec($filename);

But didn't get any output. I tried this function with another command, it works fine. Your suggestions would be highly appreciated. Thanks

like image 906
Nikunj K. Avatar asked Sep 30 '13 07:09

Nikunj K.


2 Answers

The main issue was of path and permission. I have gotten my batch file to execute.

Here is my solution:

  1. I run my batch file from the same folder the php file is in.

    exec("mybatch.bat");

  2. I make sure that Apache Service has enough permission to run the batch file. Just to test i used an administrator account for Apache to log on with.

like image 147
Nikunj K. Avatar answered Nov 15 '22 10:11

Nikunj K.


On Windows server mind the quotes. This is what works for me:

system('cmd.exe /c C:\myfolder\_batches\run_this_batch.bat');
like image 44
Milan Avatar answered Nov 15 '22 12:11

Milan