homepage Welcome to WebmasterWorld Guest from 54.204.141.129
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




msg:3387198
 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




msg:3387239
 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




msg:3387269
 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




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

Try just this:

exec("test.bat");

panic




msg:3387280
 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




msg:3387281
 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




msg:3387300
 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




msg:3387310
 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




msg:3391962
 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