Show file date in PHP

Asked

Viewed 865 times

0

I am trying to show the date of the respective files that are inside a directory, however it is showing only the date of the last file in all the files... Any idea?

<?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> ";
echo "<td> <th>Data</th> </td></tr>";

$data = date("d/m/Y H:i:s", filemtime("$path"));
// 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>
        <td>
       ".$data."
        </td>
      </tr>";
}
// Fecha a tabela
echo "</table>";
?>
  • 1

    Well, the variable $data is not changed within the repeat loop, so it is expected that the same value will be displayed. You should not update its value for each file?

  • The variable $data is only returning the folder date in $path.

1 answer

1


The variable $data is static taking only the date of the folder assigned in the variable $path.

Use the function getMTime() to take the dates of the last file modification and format them with date().

Change the line:

".$data."

For:

".date("d/m/Y H:i:s", $fileInfo->getMTime())."
  • Thank you, I removed the variable and put it straight as you showed. Missing fileInfo rsrs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.