Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP stdout not written

Tags:

php

stdout

apache

I am writing a PHP script running on an Apache httpd web server (not administrated by me). In a function, the variable $outfile is given as function parameter, and the very beginning of the function is this:

if (isset($outfile))
    $fh = fopen($outfile, 'wb');
else
    $fh = fopen('php://stdout', 'w');
echo 'Hello, ';
fwrite($fh, 'World');

I call this function with $outfile = NULL, so $fh should point to stdout. I can strip down this even more by dropping the condition:

$fh = fopen('php://stdout', 'w');
echo 'Hello, ';
fwrite($fh, 'world');

The point is, it only outputs

Hello,

instead of what I am expecting:

Hello, world!

How come and how can I circumvent this problem?

like image 987
rexkogitans Avatar asked May 10 '26 19:05

rexkogitans


1 Answers

Use php://output instead of php://stdout. As the documentation says

php://output is a write-only stream that allows you to write to the output buffer mechanism in the same way as print and echo.

php://stdout writes directly to the stdout file descriptor, so it won't be properly synchronized with the buffered output of echo.

like image 115
Barmar Avatar answered May 12 '26 09:05

Barmar