List directory files on a table using php

0

I have this php and html code that lists files in a folder, but I would like to put the results in a table, but for each row the column title is being repeated, the DirectoryIterator I was able to resolve, follows the updated code: p>

   <head>
<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
</head>
<body>  

<?php


$path = "arquivos/";


echo "<h2>Lista de Arquivos:</h2><br />";
foreach (new DirectoryIterator($path) as $fileInfo) {
        if($fileInfo->isDot()) continue;

    echo "<table>

    <tr>
    <th>Nome</th>
    </tr>
    <tr>
    <td><a href='".$path.$fileInfo->getFilename() ."'>".$fileInfo->getFilename()."</a><br /></td>
    </tr>

</table>";
}
?>
</body>
</html>

<?php
    
asked by anonymous 02.05.2018 / 04:04

1 answer

3

You will only create a table and a line of titles, so you can not repeat this, nor can you repeat the closing of table ( </table> ).

Example:

<?php

$path = "arquivos/";

// Título
echo "<h2>Lista de Arquivos:</h2><br />";

// Abre a tabela, cria títulos
echo "<table>";
echo "<tr> <th>Nome</th> </tr>";

// Loop que gera registros
foreach (new DirectoryIterator($path) as $fileInfo) {

    if($fileInfo->isDot()) continue;

    // Imprime linhas de registros
    echo "<tr>
            <td>
                <a href='".$path.$fileInfo->getFilename() ."'>".$fileInfo->getFilename()."</a><br/>
            </td>
          </tr>";
}

// Fecha a tabela
echo "</table>";

?>
    
02.05.2018 / 12:35