display html files from a directory in a php


I have a directory with html files that are articles (no header or footer, I want to show all those html files in a php called articles.php with a link to access them (the article.php file has a header and footer by require ()

Finally, what I want is to show the article as articulo.php? articulpo.html but with the header and footer of the article php file

asked by Marlon Camacho Polo 14.07.2016 в 17:53

2 answers


For this question, I'm going to imagine that your website has a structure like this (it's probably a bit different, adapting the code should to your structure should be simple):

├ index.php
├ articulo.php
├ encabezado.php
├ pie.php
└ articulos/
     ├ articulo1.html
     ├ articulo2.html
     ├ articulo3.html
     ├ ...

In your file articulo.php is where you have the header and foot with a require() , then what you should do is the following:

  • Pass the article that you want included in the header
  • Read in% co_of% what article
  • Add the article's html using $_GET or require() (as you're already doing for the header and footer)
  • Whereas the article would be passed in the header as follows: include() , then this would be done as follows in articulo.php?articulo=articulo1 ,:

        // incluimos la cabecera
        // si se ha pasado el articulo y existe su fichero html
        $articulo = filter_input(INPUT_GET, "articulo"); 
        if (isset($_GET["articulo"]) && file_exists("articulos/$articulo.html")) {
            // se incluye en la página
        } else {
            // si no se ha pasado id del artículo o no existe, se muestra un error
            echo "Artículo no especificado o no existente.";
        // incluimos el pie
    answered by 14.07.2016 в 22:16
    $extension = ".html";
    $directorio = '/tmp';
    $dir = opendir($directorio);
    while ($archivo = readdir($dir)){
     if (!is_dir($archivo)){//Validamos q no se abra otra carpeta
        if(strpos($archivo, $extension)){
    answered by 14.07.2016 в 18:30