'PHP file_get_contents function

I am developing php-based websites. When I try to load a file from the same directory using file_get_contents, the server did return some string, but php code of that file are also returned. I want the file after compiled by php, not the native code. How can I fix this problem?



Solution 1:[1]

Use this instead:

include "file.php";

Solution 2:[2]

call the file_get_contents on the url and not on the path

Solution 3:[3]

Try to use the include() function instead.

Solution 4:[4]

When wanting use the contents of a PHP file for example to create a pdf, and you want the parsed output, not the code, one can trap the output buffer of the include operation.

ob_start();
include 'something.php';
$html = ob_get_clean();
ob_end_clean();
$this->mpdf->WriteHTML($html);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nil'z
Solution 2 user
Solution 3 Harm Wellink
Solution 4 Peter Brand