Filesystem 函数
在线手册:中文 英文
PHP手册

filectime

(PHP 4, PHP 5)

filectime取得文件的 inode 修改时间

说明

int filectime ( string $filename )

返回文件上次 inode 被修改的时间,如果出错则返回 FALSE。时间以 Unix 时间戳的方式返回。

注意:在大多数 Unix 文件系统中,当一个文件的 inode 数据被改变时则该文件被认为是修改了。也就是说,当文件的权限,所有者,所有组或其它 inode 中的元数据被更新时。参见 filemtime()(这才是你想用于在 Web 页面中建立“最后更新时间”脚注的函数)和 fileatime()

注意某些 Unix 说明文本中把 ctime 说成是该文件建立的时间,这是错的。在大多数 Unix 文件系统中没有 Unix 文件的建立时间。

Note: 此函数的结果会被缓存。参见 clearstatcache() 以获得更多细节。

Tip

自 PHP 5.0.0 起, 此函数也用于某些 URL 包装器。请参见 Supported Protocols and Wrappers以获得支持 stat() 系列函数功能的包装器列表。

Example #1 一个 fileatime() 的例子

<?php

// 输出类似:somefile.txt was last changed: December 29 2002 22:16:23.

$filename 'somefile.txt';
if (
file_exists($filename)) {
    echo 
"$filename was last changed: " date("F d Y H:i:s."filectime($filename));
}

?>

参见 filemtime()

参数

filename

Path to the file.

返回值

Returns the time the file was last changed, 或者在失败时返回 FALSE. The time is returned as a Unix timestamp.

范例

Example #2 A filectime() example

<?php

// outputs e.g.  somefile.txt was last changed: December 29 2002 22:16:23.

$filename 'somefile.txt';
if (
file_exists($filename)) {
    echo 
"$filename was last changed: " date("F d Y H:i:s."filectime($filename));
}

?>

错误/异常

失败时抛出E_WARNING警告.

注释

Note:

Note: In most Unix filesystems, a file is considered changed when its inode data is changed; that is, when the permissions, owner, group, or other metadata from the inode is updated. See also filemtime() (which is what you want to use when you want to create "Last Modified" footers on web pages) and fileatime().

Note:

Note also that in some Unix texts the ctime of a file is referred to as being the creation time of the file. This is wrong. There is no creation time for Unix files in most Unix filesystems.

Note:

注意:不同文件系统对时间的判断方法可能是不相同的。

Note: 此函数的结果会被缓存。参见 clearstatcache() 以获得更多细节。

Tip

自 PHP 5.0.0 起, 此函数也用于某些 URL 包装器。请参见 Supported Protocols and Wrappers以获得支持 stat() 系列函数功能的包装器列表。

参见


Filesystem 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: 取得文件的 inode 修改时间

用户评论:

coolkoon at gmail dot com (23-Jan-2011 10:42)

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".

m dot rabe at directbox dot com (07-Jan-2010 02:07)

Under Windows you can use fileatime() instead of filectime().

soapergem at gmail dot com (05-Sep-2009 05:19)

Note that on Windows systems, filectime will show the file creation time, as there is no such thing as "change time" in Windows.

javi at live dot com (03-Feb-2009 10:15)

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;   
}

?>

rich at rmbwebs dot com (11-Mar-2008 08:07)

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 />';
}
?>

simraLIAS at mac dot com (30-Nov-2007 11: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 />';
}
?>

chuck dot reeves at gmail dot com (02-Oct-2007 05:14)

filectime running on windows reading a file from a samba share, will still show the last modified date.

website at us dot kaspersky dot com (31-Aug-2007 06: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.

StevieMc at example dot com (15-Nov-2006 02: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.

gyrbo[at]yahoo[dot]com (22-Sep-2002 12: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.

laurent dot pireyn at wanadoo dot be (27-Sep-2001 01: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.