(PHP 4, PHP 5, PHP 7, PHP 8)
filectime — Devuelve la fecha de última modificación del inodo de un fichero
Devuelve la fecha de última modificación del inodo de un fichero.
filenameRuta de acceso al fichero.
Devuelve la fecha en la que el inodo fue modificado por
última vez o false si ocurre un error. La hora
se devuelve en formato timestamp Unix.
En caso de fallo, se emitirá una advertencia de tipo E_WARNING .
Ejemplo #1 Ejemplo con filectime()
<?php
// Muestra: somefile.txt fue modificado el: December 29 2002 22:16:23.
$filename = 'somefile.txt';
if (file_exists($filename)) {
echo "$filename fue modificado el: " . date("F d Y H:i:s.", filectime($filename));
}
?>Nota:
En la mayoría de servidores UNIX, un fichero se considera modificado si los datos de su inodo son modificados. Es decir, cuando los permisos (de usuario, grupo u otros) han sido modificados. Véase también filemtime() (que puede ser utilizado cuando se creen indicaciones como "Última modificación: " en las páginas web) y fileatime() .
Nota:
Tenga en cuenta que en algunos sistemas UNIX, el
ctimede un fichero de texto es considerado como su fecha de creación. ¡Esto es falso! No existe una fecha de creación de fichero en la mayoría de los sistemas UNIX.
Nota: Tenga en cuenta que la precisión temporal puede variar según el sistema de archivos utilizado.
Nota: Los resultados de esta función se almacenan en caché. Véase la función clearstatcache() para más detalles.
A partir de PHP 5.0.0, esta función también puede ser utilizada con algunos protocolos url. Lea Protocolos y Envolturas soportados para conocer los protocolos que soportan la familia de funcionalidades de stat() .
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.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.Filemtime seems to return the date of the EARLIEST modified file inside a folder, so this is a recursive function to return the date of the LAST (most recently) modified file inside a folder.
<?php
// Only take into account those files whose extensions you want to show.
$allowedExtensions = array(
'zip',
'rar',
'pdf',
'txt'
);
function filemtime_r($path)
{
global $allowedExtensions;
if (!file_exists($path))
return 0;
$extension = end(explode(".", $path));
if (is_file($path) && in_array($extension, $allowedExtensions))
return filemtime($path);
$ret = 0;
foreach (glob($path."/*") as $fn)
{
if (filemtime_r($fn) > $ret)
$ret = filemtime_r($fn);
// This will return a timestamp, you will have to use date().
}
return $ret;
}
?>You should avoid feeding the function files without a path. This applies for filemtime() and possibly fileatime() as well. If you omit the path the command will fail with the warning "filectime(): stat failed for filename.php".Under Windows you can use fileatime() instead of filectime().filectime running on windows reading a file from a samba share, will still show the last modified date.If you need file creation time on Mac OS X:
<?php
if ($handle = popen('stat -f %B ' . escapeshellarg($filename), 'r')) {
$btime = trim(fread($handle, 100));
echo strftime("btime: %Y.%m.%d %H:%M:%S\n", $btime);
pclose($handle);
}
?>Note that on Windows systems, filectime will show the file creation time, as there is no such thing as "change time" in Windows.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.