70

Is there a way I can echo the whole content of a .html file in PHP?

For example, I have some sample.html file, and I want to echo that filename, so its content should be displayed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nikola Nastevski
  • 917
  • 5
  • 14
  • 25
  • 4
    `Include` should do .) or `readfile()`. – Vyktor Mar 02 '12 at 20:24
  • possible duplicate of [Include whole content of a file and echo it](http://stackoverflow.com/questions/921479/include-whole-content-of-a-file-and-echo-it) – trejder Jan 16 '15 at 13:41

3 Answers3

119

You should use readfile():

readfile("/path/to/file");

This will read the file and send it to the browser in one command. This is essentially the same as:

echo file_get_contents("/path/to/file");

except that file_get_contents() may cause the script to crash for large files, while readfile() won't.

Frxstrem
  • 38,761
  • 9
  • 79
  • 119
  • Is there any way I can first check if that file exists in the folder, and only if it does, to execute the above command? – Nikola Nastevski Mar 02 '12 at 20:43
  • [`file_exists()`](http://php.net/file_exists) can check whether a file or directory by that name exists, and [`is_file()`](http://php.net/is_file) can be used to check that a file exists *and is a regular file*. In you case, `is_file()` should probably be used. – Frxstrem Mar 02 '12 at 20:46
  • 1
    Strange, but when I change the file_get_contents into readfile, it adds 1191 at the bottom, and I have no idea where that number comes from :) Any ideas? – Nikola Nastevski Mar 02 '12 at 20:48
  • 2
    Do you use `echo readfile()`? Because `readfile()` returns the length of the file (which seems to be 1191 bytes), if you add `echo` before `readfile()`, it will print the length *after* you print the file. Simply remove `echo` and it should work. :) – Frxstrem Mar 02 '12 at 20:51
  • Hahaha what a simple and correct explanation, funny thing thoug :) Thank you so much (and thanks goes to the other guys as well), you helped me a lot, thanks again. – Nikola Nastevski Mar 02 '12 at 20:54
19

Just use:

<?php
    include("/path/to/file.html");
?>

That will echo it as well. This also has the benefit of executing any PHP in the file.

If you need to do anything with the contents, use file_get_contents(),

For example,

<?php
    $pagecontents = file_get_contents("/path/to/file.html");

    echo str_replace("Banana", "Pineapple", $pagecontents);

?>

This doesn't execute code in that file, so be careful if you expect that to work.

I usually use:

include($_SERVER['DOCUMENT_ROOT']."/path/to/file/as/in/url.html");

as then I can move files without breaking the includes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
  • Thhanks for the quick response :) Can you write me an example, please? I edited my question. Thanks. P.S. You have no code after: "Just use:" ? – Nikola Nastevski Mar 02 '12 at 20:26
  • 1
    Ah, forgot to indent it properly! – Rich Bradshaw Mar 02 '12 at 20:26
  • `readfile` works, but `include` doesn't seem to work for images.....`header('Content-Type: image/png'); include('test.png');exit;` shows a "broken" image. – Pacerier Oct 10 '14 at 04:17
  • Note that you should not use `include` for echoing the content of arbitrary files. If an attacker manages to upload a PHP file and you call `include` on it, you've got yourself a remote code execution exploit. Use `readfile()` instead. – cbr May 05 '22 at 14:32
14

If you want to make sure the HTML file doesn't contain any PHP code and will not be executed as PHP, do not use include or require. Simply do:

echo file_get_contents("/path/to/file.html");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Not_a_Golfer
  • 47,012
  • 14
  • 126
  • 92