Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.php

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 12-26-2006, 11:54 PM
ajh
Guest
 
Posts: n/a
Default exec() incomplete $output

Using Ubuntu 5, Apache2 and PHP 4 I am trying display in a web browser
the output from a program I wrote in C.

Here is an abridged version of my code (I can post all of it later):

<?php

exec( "myProg -a paramA -b paramB", $output);

print "<p>Returned: $return</p>";

foreach ( $output as $val ) {
print "$val<br />";
}

?>

The problem is that in the web browser only the first two lines of the
$output appear when there should almost always more than that (usually
around 10).

I have done a test with php CLI using the same code as above: php -f
mycode. The right/expected ouput comes out here.

Any ideas how this different behaviour might be happening? Wondering if
there is some default limit on the number of lines displayed by php in
the browser... or if the actual lines are too long (but 100 chars isn't
too much is it?). Thanks.

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 12-27-2006, 09:11 AM
Erwin Moller
Guest
 
Posts: n/a
Default Re: exec() incomplete $output

ajh wrote:

> Using Ubuntu 5, Apache2 and PHP 4 I am trying display in a web browser
> the output from a program I wrote in C.
>
> Here is an abridged version of my code (I can post all of it later):
>
> <?php
>
> exec( "myProg -a paramA -b paramB", $output);
>
> print "<p>Returned: $return</p>";


You forgot $return in your above call. I expect you did strip your example
here. If not you would probably have received a notice about it.
(You DO have error_reporting on to the max, right? Check php.ini or make
sure you see all errors by calling ini_set() above your script.)

>
> foreach ( $output as $val ) {
> print "$val<br />";
> }


That should work.
It prints literally to the browser what the program returned.
However, this output could contain things like < starting a html-tag.

So if you actually want to see the output generated in a browser, try this:

foreach ( $output as $val ) {
echo htmlentities($val)."<br>";
}

If this wasn't the case, make sure you have error_reporting on, if that
doesn't help (= no errors/notices) come back here and please post more
code.

Regards,
Erwin Moller

>
> ?>
>
> The problem is that in the web browser only the first two lines of the
> $output appear when there should almost always more than that (usually
> around 10).
>
> I have done a test with php CLI using the same code as above: php -f
> mycode. The right/expected ouput comes out here.
>
> Any ideas how this different behaviour might be happening? Wondering if
> there is some default limit on the number of lines displayed by php in
> the browser... or if the actual lines are too long (but 100 chars isn't
> too much is it?). Thanks.


Reply With Quote
  #3 (permalink)  
Old 12-27-2006, 11:14 AM
ajh
Guest
 
Posts: n/a
Default Re: exec() incomplete $output

Thanks for your suggestions Erwin. I've turned on all the error
messages and done some minor debugging. Nothing that related to the
output though. The underlying program is a basic search engine - you
are right about the html output so I made that htmlentities change but
it didn't make a difference.

Here is the code:


<?php

define("PROG", "/blah/bling/query");

$crawl = "courses";

$indexfile = "$crawl.csv";
$crawldir = "$crawl";
$namesfile = "$crawl.names";
$rankfile = "$crawl.rank";
$maxhits = 10;
$querystr = escapeshellarg($_GET['query']);

// Check if querystr is present
if ($querystr!=NULL)
{
// Run the query
unset($out);
exec("echo " . $querystr . " | " . PROG . " -i $indexfile -d
$crawldir -n $namesfile -p $rankfile -m $maxhits", $out);
}

?>

<html>
<body bgcolor="#ffffff">

<br>
<br>
<form method="get" action="<?php print $_SERVER['PHP_SELF']?>">
<input name="query" type="text" size="50" maxlength="50">
<input type="button" value="submit" />
</form>

<br>
<br>

<?php
// show output
foreach ($out as $val)
{
echo htmlentities($val)."<br/>";
}
?>

</body>
</html>

Reply With Quote
  #4 (permalink)  
Old 12-29-2006, 10:56 AM
Erwin Moller
Guest
 
Posts: n/a
Default Re: exec() incomplete $output

ajh wrote:

> Thanks for your suggestions Erwin. I've turned on all the error
> messages and done some minor debugging. Nothing that related to the
> output though. The underlying program is a basic search engine - you
> are right about the html output so I made that htmlentities change but
> it didn't make a difference.
>
> Here is the code:
>
>
> <?php
>
> define("PROG", "/blah/bling/query");
>
> $crawl = "courses";
>
> $indexfile = "$crawl.csv";
> $crawldir = "$crawl";
> $namesfile = "$crawl.names";
> $rankfile = "$crawl.rank";
> $maxhits = 10;
> $querystr = escapeshellarg($_GET['query']);
>
> // Check if querystr is present
> if ($querystr!=NULL)
> {
> // Run the query
> unset($out);
> exec("echo " . $querystr . " | " . PROG . " -i $indexfile -d
> $crawldir -n $namesfile -p $rankfile -m $maxhits", $out);
> }
>
> ?>
>
> <html>
> <body bgcolor="#ffffff">
>
> <br>
> <br>
> <form method="get" action="<?php print $_SERVER['PHP_SELF']?>">
> <input name="query" type="text" size="50" maxlength="50">
> <input type="button" value="submit" />
> </form>
>
> <br>
> <br>
>
> <?php
> // show output
> foreach ($out as $val)
> {
> echo htmlentities($val)."<br/>";
> }
> ?>
>
> </body>
> </html>


Hi,

Well, that is strange indeed.
As far as I can see your approach works, and I do not see any reason why a
call to the program works from a shell, but not via PHP.

2 things you can try:
1) Environment:
Before executing the command, create it in a string and echo that.
Copy/paste it into a shell, WITH the same environment as your PHP script,
and see if that works as expected. (use phpinfo() to find settings of
interest)
Some missing environmentvariable could maybe screw up the execution of your
program?

2) Catch the result again as you did halfheartedly ;-) in your first
posting.
Do add the $result again as third argument to the exec(), and see if that
contains anything of interest after you called exec.
It could possibly contain some error.

Regards,
Erwin Moller
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Incomplete Gamma function David L Cassell Newsgroup comp.soft-sys.sas 0 11-16-2005 07:17 PM
Re: Incomplete Gamma function Hongjie Wang Newsgroup comp.soft-sys.sas 0 11-16-2005 01:39 PM



All times are GMT. The time now is 05:48 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.