I’m using the following function to open a file:
function example() {
$foo = fopen('file.txt', 'r');
while (!feof($foo)) {
$foo2 = fgets($foo);
echo $foo2;
}
}
Here’s the code where it’s called:
<?php
include($_SERVER['DOCUMENT_ROOT']."/class_lib.php");
$page = new Page();
function example() {
$foo = fopen('file.txt', 'r');
while (!feof($foo)) {
$foo2 = fgets($foo);
echo $foo2;
}
}
$page->meta = array
(
'title' => 'snip',
'description' => 'snip'
);
$page->content = "
snipsnipsnipsnipsnipsnip
<div id=\"foo\">
<pre>
".example()."
</pre>
</div>
<br/>snipsnip
";
$page->Display();
?>
For some reason, even though the function is called within the pre element, it appears at the start of the page (the file is output, then the html is loaded). Same thing happens when I use include(). I must overlooking something obvious… any ideas?
Here’s the class_lib.php if it’s needed: http://pastebin.com/7euqEWNq
You use
echowhen reading your file, so it’s directly printed out.You can used output buffering : ob_start
Or simply get the file content and use it instead or you function call : file_get_contents