This is a modification of simraLIAS at mac dot com's code.
Modification dates should not be used for keys in an array when sorting by date because there is no guarantee that all files will have different dates. Collisions resulting in files missing from the list could be possible. A better way is to use the filename as the key (guaranteed to not be collisions)
<?php
foreach (glob("../downloads/*") as $path) { //configure path
$docs[$path] = filectime($path);
} asort($docs); // sort by value, preserving keys
foreach ($docs as $path => $timestamp) {
print date("d. M. Y: ", $timestamp);
print '<a href="'. $path .'">'. basename($path) .'</a><br />';
}
?>
filectime
(PHP 4, PHP 5)
filectime — Obtiene la hora de modificación del inode del archivo
Descripción
Obtiene la hora de modificación del inode de un archivo.
Lista de parámetros
- nombre_archivo
-
Ruta al archivo.
Valores retornados
Devuelve la hora en que el archivo fue cambiado por última vez, o FALSE en caso de fallo. La hora es devuelta como una marca de tiempo Unix.
Ejemplos
Example #1 Un ejemplo de filectime()
<?php
// imprime p.ej. un_archivo.txt fue modificado en: December 29 2002 22:16:23.
$nombre_archivo = 'un_archivo.txt';
if (file_exists($nombre_archivo)) {
echo "$nombre_archivo fue modificado en: " . date("F d Y H:i:s.", filectime($nombre_archivo));
}
?>
Notes
Note: Nota: En la mayoría de sistemas de archivos en Unix, un archivo se considera cambiado cuando los datos del inode cambian; es decir, cuando la información de permisos, el dueño, grupo u otros meta-datos son actualizados. Vea también filemtime() (que es lo que probablemente quiera usar cuando desea crear pies de página tipo "Modificado por última vez" en páginas web) y fileatime().
Note: Note también que en algunos textos sobre Unix, el valor ctime de un archivo se detalla como la hora de creación del archivo. Esto es falso. No existe una hora de creación para archivos Unix en la mayoría de sistemas de archivos usados en Unix.
Note: Los resultados de esta función son guardados. Consultar clearstatcache() para más detalles.
A partir de PHP 5.0.0, esta funcion tambien puede usarse con algunas URL como nombre de fichero. Consultar Lista de Protocolos/Envolturas Soportadas, para obtener una lista con soporte para la funcionalidad stat().
filectime
11-Mar-2008 01:07
30-Nov-2007 03:24
This is another way to get a list of files ordered by upload time:
<?php
foreach (glob("../downloads/*") as $path) { //configure path
$docs[filectime($path)] = $path;
} ksort($docs); // sort by key (timestamp)
foreach ($docs as $timestamp => $path) {
print date("d. M. Y: ", $timestamp);
print '<a href="'. $path .'">'. basename($path) .'</a><br />';
}
?>
02-Oct-2007 09:14
filectime running on windows reading a file from a samba share, will still show the last modified date.
31-Aug-2007 10:51
Line 37 of the code above has an error.
echo "File name: $file - Date Added: $date. <br/>"";
There is an extra " after the <br/> that needs to be deleted in order for this code to work.
15-Nov-2006 06:28
This method gets all the files in a directory, and echoes them in the order of the date they were added (by ftp or whatever).
<?PHP
function dirList ($directory, $sortOrder){
//Get each file and add its details to two arrays
$results = array();
$handler = opendir($directory);
while ($file = readdir($handler)) {
if ($file != '.' && $file != '..' && $file != "robots.txt" && $file != ".htaccess"){
$currentModified = filectime($directory."/".$file);
$file_names[] = $file;
$file_dates[] = $currentModified;
}
}
closedir($handler);
//Sort the date array by preferred order
if ($sortOrder == "newestFirst"){
arsort($file_dates);
}else{
asort($file_dates);
}
//Match file_names array to file_dates array
$file_names_Array = array_keys($file_dates);
foreach ($file_names_Array as $idx => $name) $name=$file_names[$name];
$file_dates = array_merge($file_dates);
$i = 0;
//Loop through dates array and then echo the list
foreach ($file_dates as $file_dates){
$date = $file_dates;
$j = $file_names_Array[$i];
$file = $file_names[$j];
$i++;
echo "File name: $file - Date Added: $date. <br/>"";
}
}
?>
I hope this is useful to somebody.
21-Sep-2002 04:35
filectime doesn't seem to be working properly on Win32 systems (it seems to return the creation time). Try using filemtime if you have problems.
27-Sep-2001 05:01
If you use filectime with a symbolic link, you will get the change time of the file actually linked to. To get informations about the link self, use lstat.
