PHP:为什么我的输出缓冲不起作用?

I am using output buffering in two different paths, the code is basically the same however one of them never returns the value, why?

version.php:

<?php

    echo "v1.1.8";

working.php:

<?php

    ob_start();
    include dirname(__FILE__)  . "/version.php";
    $version = ob_get_contents();
    ob_end_clean();

    // returns "v1.1.8"
    return $version;

notWorking.php:

<?php

    ob_start();
    include dirname(__FILE__) . "/../../../../version.php";
    $version = ob_get_contents();
    ob_end_clean();

    // returns empty
    return $version;

Directory structure, printed by tree command:

.
|-- version.php
|-- working.php
|-- directory
|   |-- directory
|   |   |-- directory
|   |   |   |-- directory
|   |   |   |   |-- notWorking.php

Environment details:

  • PHP Version 5.2.17
  • Ubuntu 12.04.5 LTS

Given the code is basically the same. i guess you did not get the right path to the second file.

In order to debug this run the following code

// should return the full path to the file
die(realpath(dirname(__FILE__)  . "/../../../../version.php"));

if the command above returns false, then you most like got the path to the file wrong.