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.

2

3 Answers 3

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.

5
  • 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? Mar 2, 2012 at 20:43
  • file_exists() can check whether a file or directory by that name exists, and 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 2, 2012 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? Mar 2, 2012 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 2, 2012 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. Mar 2, 2012 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.

4
  • 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:" ? Mar 2, 2012 at 20:26
  • 1
    Ah, forgot to indent it properly! Mar 2, 2012 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, 2014 at 4: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 5, 2022 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");
0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.