Page 1 of 1

How to execute batch file via PHP?

Posted: Mon Oct 20, 2008 1:41 am
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

Re: How to execute batch file via PHP?

Posted: Mon Oct 20, 2008 2:05 am
by jaoudestudios
What is in your test.bat file?

Re: How to execute batch file via PHP?

Posted: Mon Oct 20, 2008 2:07 am
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?

Re: How to execute batch file via PHP?

Posted: Mon Oct 20, 2008 2:13 am
by pcoder
The batch file contains:
iexplore.exe "http://www.google.com"

Re: How to execute batch file via PHP?

Posted: Mon Oct 20, 2008 2:39 am
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.

Re: How to execute batch file via PHP?

Posted: Mon Oct 20, 2008 2:47 am
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... :|

Re: How to execute batch file via PHP?

Posted: Mon Oct 20, 2008 3:32 am
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??

Re: How to execute batch file via PHP?

Posted: Thu Oct 23, 2008 10:24 pm
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.