24

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...

sp2
  • 509
  • 1
  • 5
  • 13

4 Answers4

50

Use the include PHP function for each html file.

example:

<?php
// do php stuff

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

?>
Bashevis
  • 1,507
  • 16
  • 21
  • 6
    If the html files represent truly static content, you should use [readfile()](http://php.net/manual/en/function.readfile.php). 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. – Kip Feb 05 '17 at 20:04
27

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');

?>
Kip
  • 107,154
  • 87
  • 232
  • 265
  • I additionally needed to call `ob_start()` and `ob_end_flush()` before and after the `readfile` call respectively to avoid corrupt output. – beerbajay Oct 28 '19 at 22:37
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>';
    }
?>
Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82
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

Community
  • 1
  • 1
Sam Janssens
  • 1,491
  • 1
  • 12
  • 30
  • 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 http://localhost/3.html but then also it dint work out – sp2 Sep 06 '12 at 07:02
  • u cannot even open the file by simply browsing to it ? is this what u are saying ? – Sam Janssens Sep 06 '12 at 07:48
  • ya when i try to open it using localhost, since i m using xampp server. it doesnot work – sp2 Sep 06 '12 at 11:09
  • 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 ... – Sam Janssens Sep 06 '12 at 11:12