homepage Welcome to WebmasterWorld Guest from 54.196.62.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Launching an external command prompt
panic

10+ Year Member



 
Msg#: 3387196 posted 7:52 pm on Jul 5, 2007 (gmt 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

 

mcavic

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3387196 posted 8:34 pm on Jul 5, 2007 (gmt 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);

panic

10+ Year Member



 
Msg#: 3387196 posted 9:12 pm on Jul 5, 2007 (gmt 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?

eelixduppy

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



 
Msg#: 3387196 posted 9:24 pm on Jul 5, 2007 (gmt 0)

Try just this:

exec("test.bat");

panic

10+ Year Member



 
Msg#: 3387196 posted 9:36 pm on Jul 5, 2007 (gmt 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.

eelixduppy

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



 
Msg#: 3387196 posted 9:39 pm on Jul 5, 2007 (gmt 0)

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+ Year Member



 
Msg#: 3387196 posted 10:02 pm on Jul 5, 2007 (gmt 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.

eelixduppy

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



 
Msg#: 3387196 posted 10:11 pm on Jul 5, 2007 (gmt 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);

panic

10+ Year Member



 
Msg#: 3387196 posted 12:08 am on Jul 12, 2007 (gmt 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?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved