2

I have two php files. Let's call them a.php and b.php.

When the user visits a.php, I use exec() to execute b.php and b.php generates an output.

My question is: how can I get this output displayed at a.php or to another file when the process is complete in b.php ?

Here is my code:

exec("C:\wamp\bin\php\php5.5.12\php.exe C:\wamp\www\b.php 2>otst.txt");
Getz
  • 3,983
  • 6
  • 35
  • 52
Akshay Khetrapal
  • 2,586
  • 5
  • 22
  • 38
  • 1
    exec() blocks, usually, until the exec'd process completes. that means your `a` script is dead in the water until `b` completes. but by default `exec` only returns the LAST line of output from the program. to capture everything, you need to do `exec(external_file, $output, $exit_value)`. – Marc B Apr 16 '15 at 20:27
  • The OP should precise the question, in particular the term "display at" is not really appropriate for php files. – Ratbert Apr 16 '15 at 20:51
  • Yeah, my bad. I'll update the question. – Akshay Khetrapal Apr 16 '15 at 20:52

2 Answers2

0

Use one of theses 2 PHP function :

passthru - Execute an external program and display raw output

system- Execute an external program and display the output

Example with System :

<?php
echo '<pre>';

// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('ls', $retval);

// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>

(Source http://php.net/manual/en/function.system.php )

user2267379
  • 1,067
  • 2
  • 10
  • 20
0

If you don't need anything fancy, you can use the backtick (`) operator instead of exec():

$output = `php b.php`;

Note that the backticks correspond to calling shell_exec, see this related question for a discusson on exec vs shell_exec.

Community
  • 1
  • 1
Michael Jaros
  • 4,586
  • 1
  • 22
  • 39