Executing a BAT file from PHP using exec()

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
Alaskan57
Forum Newbie
Posts: 2
Joined: Fri Dec 18, 2009 1:31 pm

Executing a BAT file from PHP using exec()

Post by Alaskan57 »

Hello everyone,

I'm tearing out my hair on this one. I've looked all over the internet and I have no idea why my code isn't working. What I'm trying to do is run a batch file (.bat) from within PHP. I have PHP 5.2.14 running on IIS on my Windows 7 machine. One of the examples I looked on online that's used to check if exec is working had this code:

Code: Select all

exec('dir', $response);
foreach($response as $line) {
    echo $line . "<br>";
}
When I run that code locally it prints out my directory structure in my wamp folder (I named it wamp even though I'm running IIS since I'm used to looking in the wamp folder for localhost :) ). So it doesn't seem like exec is disabled. Anyway, when I try to run the following code:

Code: Select all

exec("cmd.exe /c notathome.bat");
it just hangs there as if it's trying to load something. That batch file is located in the same directory as the PHP code that's trying to run it and definitely works because I can just click on it and it opens the calculator.

I've tried this using other functions like system(), popen(), and even tried my hand at the Windows Shell with similar results. I'm sure I'm just missing a small something. Anyone have any idea?
Post Reply