-1

I'm trying to read a small html file but when I echo the results I don't see anything. The read/display code is:

    $headerName = "header.html";
    $header = fread($header_fp,filesize($headerName));
    $header_filesize = filesize($headerName);
    echo "<pre>";
    echo ("<br />header: file size = $header_filesize data = $header");
    echo "</pre>";

The $header_filesize prints as 110, which is correct.

The header.html file I'm reading is:

<!doctype html> 
<!-- HTML5  -->
<html>
<head>
<meta charset="utf-8">
<title>Webplaces</title>
</head>
<body>

The above is what I was expecting to see with the echoes.

Does anyone see why the echoes don't show the file contents?

Thanks

Steve
  • 4,534
  • 9
  • 52
  • 110
  • 3
    I'm pretty sure you are rendering it in a browser. So it renders as HTML. Check the source or `htmlspecialchars()` that shit. – PeeHaa Jul 12 '13 at 07:56
  • True that, your rendering non-display tags, if you view the source of the html in the browser you will likely see it. If your looking for a slightly easier solution to reading the file try `file_get_contents`, but it is not so different to what you are doing already. – Flosculus Jul 12 '13 at 07:58

3 Answers3

2

Do you escape the html-chars? if you just echo the content it will be handles as normal html and you cannot see it in output.

Try this:

echo htmlspecialchars($header, ENT_QUOTES, 'UTF-8');
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
DaKirsche
  • 352
  • 1
  • 14
  • 1
    Yes, htmlspecialchars() displays the HTML as I want. The problem, as everyone has said, is that the echo goes to the browser and the browser doesn't display tags. – Steve Jul 12 '13 at 08:36
1

Did you try View Source? HTML gets rendered in a browser.

That said, why are you doing all that? Just use readfile or (if you want PHP to be run) include.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 1
    readfile (and include) echo the output to the browser. That's not at all what I want to do. I want to read the file, combinine it with something I've ajaxed up to the server, and write the combination to a new file. – Steve Jul 12 '13 at 08:27
  • Then try [`file_get_contents`](http://php.net/file_get_contents) – Niet the Dark Absol Jul 12 '13 at 08:29
0

fread() doesn' t take a filename, rather then a filesystem pointer!

You have to use fopen() before hand:

$header_handle = fopen($header_name, "r");
$header = fread($header_handle, filesize($header_name);
// ...

Your mistake was to forget fopen() the file.

And don' t forget to fclose($header_handle);!

DAG
  • 6,710
  • 4
  • 39
  • 63
  • 1
    No. $header_fp in $header = fread($header_fp,filesize($headerName)); is a file pointer. I didn't show the $header_fp = fopen("$headerName", "r"); – Steve Jul 12 '13 at 08:12
  • Then post your complete code in the future please. – DAG Jul 12 '13 at 10:29