PHP config output_buffering and headers

I’m trying to post a bug but keep getting rejected for having a link in the post, however I can’t see any links. So I’m posting nothing and may try again a 2nd time.

I already have a solution to the problem, but I can’t communicate it here because of the spam rules.

On my dev PC I have PHP ini setting output_buffering = Off

On production server we have output_buffering=4096

Normal behaviour is that if output_buffering is off, headers are sent prematurely and people get errors when they try to send headers in their code.

I have encountered the reverse problem but only when then body content is smaller than about 1000 characters. I am loading JS via XHR call, and the Yii action sets header(‘content-type: text/javascript’); prior to echoing the JS file line by line, full script is:

I’ll now try to post the code which is producing this strange result:

class GetcontrollerController extends Controller{

public $json;

public function beforeAction( $event )


    $this->enableCsrfValidation = false;

    $this->json = Json::decode(file_get_contents(""), false); // content of "" removed because of forum spam rules, was php: (slash) (slash) input

    header('content-type: text/javascript');

    return parent::beforeAction( $event );


public function actions()


    return [

        'error' => [

            'class' => 'yii\web\ErrorAction',




public function actionIndex(){

    $file = $this->json->file;

    $handle = fopen( Yii::getAlias( "@webroot" ) . '\\js2\\controllers\\' . $file . ".js", "r" );

    while( $line = fgets( $handle ) ){

        echo $line;


    global $urlpath;



If output_buffering is off, this always works. If output_buffering is on (set to 4096) which is recommended for server config, then if the JS file is smaller than about 1000 characters, the header sent to the browser is text/html. I can fix the problem by inflating the size of the JS file with a large comment.

I’ve reproduced this for both PHP 5.5 and 5.6 on Windows, using both WAMP and PHP 5.5 on Windows 7 desktop, and using the Apache Lounge install of apache 2.4 and PHP install from for PHP 5.6 on Windows 2008R2 server. PHP is installed as an Apache (thread-safe) module.

If I run a simple script (below) to try and demonstrate the problem, there is no problem, so it could be some interaction between Yii2 and PHP etc.


header('content-type: text/javascript');

$nlines = 5;

for( $i=1; $i<$nlines; $i++ ){

    echo "line " . $i . "\n";