Reply to Re: popen on Windows

Your name:

Reply:


Posted by Csaba Gabor on 12/25/07 20:22

On Dec 23, 11:42 am, Daniel Klein <dani...@featherbrain.net> wrote:
> On Sun, 23 Dec 2007 14:50:59 GMT, Daniel Klein
>
>
>
> <dani...@featherbrain.net> wrote:
> >On Mon, 24 Dec 2007 00:44:14 +1000, "Vince Morgan"
> ><vinharAtHereoptusnet.com.au> wrote:
>
> >>"Daniel Klein" <dani...@featherbrain.net> wrote in message
> >>news:u89qm352ob3hep0heags7bd82q3seqcdjb@4ax.com...
> >>> I'm trying to get popen to work on Windows.
>
> >>> Here's a simplified example of what I'm trying to get working:
>
> >>> I have a hw.c program as follows:
>
> >>> #include <stdio.h>
> >>> main()
> >>> {
> >>> printf ("Hello World!\n");
> >>> }
>
> Just to be thorough, I created a php script:
>
> <?php
> echo "Hi";
> ?>
>
> and replaced the 'hw.exe' with 'php hi.php' - I got the same problem
> as originally reported.

There are usually two issues with this type of code: (1) Is anything
getting executed at all? (2) Collecting the output. To verify that
(1) is working, you could use a file_put_contents(...) together with
date(...)

Also, popen has undergone changes in the last year so if you are
working with an older version of php (perhaps 5.1 or earlier?) things
may be slightly different.

Here is an example of collecting output from popen that works in a web
server context for me:

<?php
function getPOpenOutput ($cmd) {
$out = "";
$proc = popen ($cmd, "r");
while (!feof($proc)) { // wait for output to finish
$slurp = fgets($proc, 256);
if (strlen($slurp)>0) $out .= $slurp;
else com_message_pump(200); }
pclose ($proc);
return $out; }

$cmnd = "SchTasks";
$out = getPOpenOutput ($cmnd);
print "<pre>$out</pre>";
?>

You may have to set the web server to
"Allow service to interact with the Desktop",
which is found under Control Panel \ Services

Finally, if you are calling on more complicated
programs, you may need to escape characters
like double quotes, <, >, ^, et. al) within
$cmnd, which can be non trivial. For example:
$cmnd = "php -r echo('hi');";
but
$cmnd = "php -r \"echo('hi there');\"";

Csaba Gabor from Vancouver

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация