Reputation: 73195
Is there a way to redirect output of an executing PHP script?
Of course this is trivial when launching the script via the command line. But how is this accomplished after the script is started?
Note: I need to capture the syntax errors and such as well.
Upvotes: 1
Views: 8610
Reputation: 5861
If you want to capture parser errors, you need to:
Make sure display_errors
is On
Set an error_prepend_string
and an error_append_string
Call ob_start
from an auto_prepend file that runs before every PHP file you might be executing.
Use set_error_handler
as you normally would, and make your callback sift through the named output buffer, looking for your custom error_prepend_string and error_append_string. If you find it, then shunt your output wherever you want it. If you don't, then let it go wherever it would normally.
Most of this can be achieved through ini_set
calls, but the auto_prepend file will need to be specified in your php.ini.
Upvotes: 1
Reputation: 784
Syntax errors will output to STDERR, not STDOUT. Make sure you also redirect STDERR in your pipline...
./myscript | myfile.php &> out.txt
Upvotes: 1
Reputation: 798656
You can use PHP's output control even in a command line script:
echo "123\n";
ob_start();
echo "456\n";
$s = ob_get_contents();
ob_end_clean();
echo "*$s";
Upvotes: 1
Reputation: 1389
PHP has file i/o functions to write to a file.
http://www.php.net/manual/en/function.fopen.php
http://www.php.net/manual/en/function.fwrite.php
Upvotes: -2