Welcome to WebmasterWorld Guest from 54.147.63.124

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Launching an external command prompt

     
7:52 pm on Jul 5, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:June 16, 2003
posts:633
votes: 0


I have a .bat file stored locally that I would like launched from PHP.

Here's the code I've tried using:

exec("cmd /K test.bat");

The PHP script that contains the code above hangs when executed. What is the correct way to do this?

Or is there a way to have PHP display the output from the .bat file on the fly (as opposed to waiting for the .bat file to execute THEN dump the output?)

-p

8:34 pm on July 5, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 31, 2003
posts:1316
votes: 0


I don't use php in Windows, but try cmd /c instead of /k

/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains

Or to display the output on the fly, try:

$pp = popen("cmd /c test.bat", "r");
while ($s = fgets($pp)) {
print "$s";
}
pclose($pp);

9:12 pm on July 5, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:June 16, 2003
posts:633
votes: 0


I've tried both /k & /c switches, but PHP still hangs.

For the sake of debugging, I emptied out test.bat just to make sure it's not the contents that are causing PHP to hang, but unfortunately PHP is still hanging.

Using the popen method produces the same result - PHP hanging.

Any idea as to what the problem might be?

9:24 pm on July 5, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


Try just this:

exec("test.bat");
9:36 pm on July 5, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:June 16, 2003
posts:633
votes: 0



Try just this:

exec("test.bat");

It executes silently, but keep in mind that I'm trying to monitor/view the output from test.bat.

9:39 pm on July 5, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


exec returns a string which is the last line in the output of the script. So then try this:

echo exec("test.bat");
10:02 pm on July 5, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:June 16, 2003
posts:633
votes: 0


exec returns a string which is the last line in the output of the script. So then try this:

echo exec("test.bat");

It doesn't hang, but it outputs after test.bat has been fully executed - not while the output from test.bat is being generated.

10:11 pm on July 5, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


Ahh, I'm sorry. I seemed to have misread that detail. Try the example above with this change:

$pp = popen("test.bat", "r");
while ($s = fgets($pp)) {
print $s;
}
pclose($pp);
12:08 am on July 12, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:June 16, 2003
posts:633
votes: 0


This seems to be working from the command line - but is there any way I can get it to output to the browser in real time?

I've seen it done before by using a text box - any idea as to how I could go about doing this?

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members