32

I have many html files and now i want to call each of the files one by one using some php code. but whenever i try to run the php code for calling those html files from the folder, it doesnot work.

    1.html view
    2.html view
    3.html view

So, 1,2 and 3 are the html files and now by clicking view user should be directed to the link. How can i solve the problem for incorporating html files in php, so that the files will be displayed to user one by one with a view option. And whenever user will click view, user will get the html page viewed.

Note: i m using xampp server in windows7

Any suggestions will be appreciated...

4
  • 1
    Do you mean you want to display html-files from a specific directory? Or just include() them? Commented Sep 6, 2012 at 6:29
  • @jitheman i want to embed html file in php Commented Sep 6, 2012 at 6:59
  • ya want to include html file in php. Commented Sep 6, 2012 at 7:00
  • If an answer answer your question, mark it, if not, tell us why. Commented May 21, 2014 at 15:18

4 Answers 4

55

Use the include PHP function for each html file.

example:

<?php
// do php stuff

include('fileOne.html');
include('fileTwo.html');

?>
Sign up to request clarification or add additional context in comments.

1 Comment

If the html files represent truly static content, you should use readfile(). If the html file happend to include PHP directives (like <?), include will try to interprent them as PHP code. readfile will just return the content to the browser.
34

Small improvement to the accepted answer: you should prefer readfile() over include() or require() for non-PHP files. This is because include() and require() will be processed as PHP code, so if they contain something that looks like PHP (like <?), they may generate errors. Or worse yet, a security vulnerability. (There is still potential security vulnerability if you are displaying user-provided HTML too, of course, but they at least won't be able to run code on your server.)

If the .html files actually contain PHP code, you should name them appropriately.

<?php
// do php stuff

readfile('fileOne.html');
readfile('fileTwo.html');

?>

2 Comments

I additionally needed to call ob_start() and ob_end_flush() before and after the readfile call respectively to avoid corrupt output.
This should be the accepted answer!
2

Using opendir and loop throught the files.

Example:

<?php

    $dir = "/tmp";
    $dh = opendir($dir);
    while(false !== ($filename = readdir($dh)))
    {
        echo $filename . ' <a href="' . $dir . '/' . $filename . '">view</a>';
    }
?>

Comments

1

see here Loop code for each file in a directory

to know how to loop the files in the folder, then just echo an A tag with a link

4 Comments

Thanks .... but the problem is when i try to run an html file in php, it doesn't work. So, i tried to open the html file separately using a web browser by typing the link localhost/3.html but then also it dint work out
u cannot even open the file by simply browsing to it ? is this what u are saying ?
ya when i try to open it using localhost, since i m using xampp server. it doesnot work
then it has nothing to do with your php code or whatever? you should setup your server to correctly serve your pages before asking how to make your page do what u want ...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.