exec() incomplete $output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ajh

    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>Returne d: $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.

  • Erwin Moller

    #2
    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>Returne d: $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($v al)."<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.

    Comment

    • ajh

      #3
      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.nam es";
      $rankfile = "$crawl.ran k";
      $maxhits = 10;
      $querystr = escapeshellarg( $_GET['query']);

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

      ?>

      <html>
      <body bgcolor="#fffff f">

      <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($v al)."<br/>";
      }
      ?>

      </body>
      </html>

      Comment

      • Erwin Moller

        #4
        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.nam es";
        $rankfile = "$crawl.ran k";
        $maxhits = 10;
        $querystr = escapeshellarg( $_GET['query']);
        >
        // Check if querystr is present
        if ($querystr!=NUL L)
        {
        // Run the query
        unset($out);
        exec("echo " . $querystr . " | " . PROG . " -i $indexfile -d
        $crawldir -n $namesfile -p $rankfile -m $maxhits", $out);
        }
        >
        ?>
        >
        <html>
        <body bgcolor="#fffff f">
        >
        <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($v al)."<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 environmentvari able 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

        Comment

        Working...