How to execute batch file via PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

How to execute batch file via PHP?

Post by pcoder »

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

Code: Select all

 
$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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: How to execute batch file via PHP?

Post by jaoudestudios »

What is in your test.bat file?
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: How to execute batch file via PHP?

Post by omniuni »

A *.bat is a batch script. Of course, it will only work on Windows, but a batch script is not a command. For example, on Linux, if I wanted to execute a shell script myscript.sh, I would need to exec('bash myscript.sh');. I do not know what the Windows command is for executing a *.bat, I am afraid.... um, maybe PsExec?
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: How to execute batch file via PHP?

Post by pcoder »

The batch file contains:
iexplore.exe "http://www.google.com"
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: How to execute batch file via PHP?

Post by jaoudestudios »

If you type...
iexplore.exe "http://www.google.com"
into the windows command line, does it work?

I am assuming it does. Check the path is correct to the file.

Are you testing on WAMP or LAMP.

Like omniuni said it would only work on WAMP.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: How to execute batch file via PHP?

Post by omniuni »

I would be extremely careful with a script like that, anyways! Imagine in some users begin executing it, and the Windows server begins opening all these instances of IE! Internet Explorer is enough of a resource hog; it would likely take down the server before long.

I am going to make the quick reminder that executing such a *.bat executes IE on the server, not the users machine... I hope I don't have to say that... :|
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: How to execute batch file via PHP?

Post by pcoder »

It works perfectly from the command line directly.
And i am using WAMP.
But, it is outside the document root. Is it possible to execute these kind of command??
ramarbstr12play
Forum Newbie
Posts: 1
Joined: Thu Oct 23, 2008 11:13 am

Re: How to execute batch file via PHP?

Post by ramarbstr12play »

I am having the same issue with batch files using Windows,Apache,Mysql,and PHP. Namely php running in appache on Windows. Basically i have gotten a little farther than you.

I was trying to execute a batch file from php using

Code: Select all

exec('c:\mydir\mybatch.bat');
after alot of tweaking and fumbling around i have gotten my batch file to execute.
1. i run my batch file from the same folder the php file is in.
i.e:

Code: Select all

exec("mybatch.bat");
2. i make sure that Apache Service has enough permission to run the batch file. Just to test i used an administartor account for Apache to log on with.

This is what i have done so far. I am able to call a batch file that uses "psexec" with a admin user account to run a program. But i get an error from windows everytime i run this php file. I cant remember the exact error and i will post it later.

But, it basically says that the program like "cmd" for example that is used to run another batch file can not run or has ancountered an error. The batch file runs perfect from Windows. But if it is called from PHP it gives an error.

I am stumped.

Thought i would post this here instead of starting another Thread. so we can both get an answer.
Post Reply