Welcome to WebmasterWorld Guest from 54.163.35.238

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Launching an external command prompt

     

panic

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

10+ Year Member



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

mcavic

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

WebmasterWorld Senior Member 10+ Year Member



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);

panic

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

10+ Year Member



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?

eelixduppy

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

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



Try just this:

exec("test.bat");

panic

9:36 pm on Jul 5, 2007 (gmt 0)

10+ Year Member




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.

eelixduppy

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

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



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

echo exec("test.bat");

panic

10:02 pm on Jul 5, 2007 (gmt 0)

10+ Year Member



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.

eelixduppy

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

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



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);

panic

12:08 am on Jul 12, 2007 (gmt 0)

10+ Year Member



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?

 

Featured Threads

Hot Threads This Week

Hot Threads This Month