在PHP开发中我们有时需要知道文件的最后访问时间和文件最后修改的时间,介绍一下PHP提供的确定文件的访问,创建和最后修改时间的3个函数:fileatime(),filectime()和filemtime()。
1、fileatime()
int fileatime(string filename)
fileatime()函数返回filename最后访问的时间,这里的最后访问是指每当一个文件的数据块被读取,采用UNIX时间戳格式,有错误时返回FALSE。
2、filectime()
int filectime(string filename)
filectime()函数返回filename最后改变的时间,这里的最后改变是指指定文件 filename 的 inode 最后改变时间,其中inode (索引节点)用来存放档案及目录的基本信息包含时间、档名、使用者及群组等,采用UNIX时间戳格式,有错误时返回FALSE。
3、filemtime()
int filemtime(string filename)
filemtime()函数返回filename最后修改的时间,最后修改指的是文件的内容改变,采用UNIX时间戳格式,有错误时返回FALSE。
例如:
<?php
$file="/software/test.txt";
echo "文件最后访问的时间是".date("Y-m-d H:i:s",fileatime($file))."<br/>";
echo "文件最后改变的时间是".date("Y-m-d H:i:s",filectime($file))."<br/>";
echo "文件最后修改的时间是".date("Y-m-d H:i:s",filemtime($file))."<br/>";
?>
远程文件
function remote_filectime($url_file){
$headInf = get_headers($url_file,1); //注意第二个参数
return strtotime($headInf['Last-Modified']);
}
get_headers返回数据
Array
(
[0] => HTTP/1.1 200 OK
[Server] => nginx
[Date] => Wed, 02 Mar 2016 07:34:52 GMT
[Content-Type] => text/xml
[Content-Length] => 2750
[Connection] => close
[Set-Cookie] => IPLOC=CN1100; expires=Thu, 02-Mar-17 07:34:52 GMT; path=/
[P3P] => CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"
[ETag] => "Ahh8eNBCjmL"
[Last-Modified] => Tue, 02 Feb 2016 09:55:40 GMT
[Accept-Ranges] => bytes
)