百科狗-知识改变命运!
--

filemtime() - 取得文件修改时间 - php 文件目录函数

乐乐1年前 (2023-11-21)阅读数 10#技术干货
文章标签时间

filemtime()

(PHP 4, PHP 5, PHP 7)

取得文件修改时间

说明

filemtime(string $filename): int

本函数返回文件中的数据块上次被写入的时间,也就是说,文件的内容上次被修改的时间。

参数

$filename

文件的路径。

返回值

返回文件上次被修改的时间,或者在失败时返回FALSE。时间以 Unix 时间戳的方式返回,可用于date()。

范例

Example #1filemtime()例子

错误/异常

失败时抛出E_WARNING警告。

注释

Note:

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

filemtime() - 取得文件修改时间 - php 文件目录函数

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

Tip

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

参见

  • filectime() 取得文件的 inode 修改时间
  • stat() 给出文件的信息
  • touch() 设定文件的访问和修改时间
  • getlastmod() 获取页面最后修改的时间
This is a very handy function for dealing with browser caching. For example, say you have a stylesheet and you want to make sure everyone has the most recent version. You could rename it every time you edit it, but that would be a pain in the ass. Instead, you can do this:

Sample output:

By appending a GET value (the UNIX timestamp) to the stylesheet URL, you make the browser think the stylesheet is dynamic, so it'll reload the stylesheet every time the modification date changes.
To get the last modification time of a directory, you can use this:
$getLastModDir = filemtime("/path/to/directory/.");
Take note on the last dot which is needed to see the directory as a file and to actually get a last modification date of it. This comes in handy when you want just one 'last updated' message on the frontpage of your website and still taking all files of your website into account. Regards, Frank Keijzers
To find the oldest file in a directory : 
$directory= "C:\\";
$smallest_time=INF;
$oldest_file='';
if ($handle = opendir($directory)) {
  while (false !== ($file = readdir($handle))) {
    $time=filemtime($directory.'/'.$file);
    if (is_file($directory.'/'.$file)) {
      if ($time 
Cheaper and dirtier way to code a cache: 
"this is not (necessarily) correct, the modification time of a directory will be the time of the last file *creation* in a directory (and not in it's sub directories)."
This is not (necessarily) correct either. In *nix the timestamp can be independently set. For example the command "touch directory" updates the timestamp of a directory without file creation.
Also file removal will update the timestamp of a directory.
If you want this functionality for the parent web page you should use getlastmod()
i.e.

within the included page... i.e. as a commont footer include for all pages
There's a deeply-seated problem with filemtime() under Windows due to the fact that it calls Windows' stat() function, which implements DST (according to this bug: http://bugs.php.net/bug.php?id=40568). The detection of DST on the time of the file is confused by whether the CURRENT time of the current system is currently under DST.
This is a fix for the mother of all annoying bugs:

Dustin Oprea
i needed the ability to grab the mod time of an image on a remote site. the following is the solution with the help of Joe Ferris. 
If you do use:

Make note that it will throw a warning as the documentation states. While tobias makes a good point for production, when error reporting is off, this can increase your performance over using `file_exists`. However, if error reporting is on and it's writing to a log... this is not the case. Obviously this varies on sever specs and the size of the error log. For most cases, it's still ideal to use the `file_exists` method.
Another little handy tool; to get the most recent modified time from files in a directory. It even does recursive directories if you set the $doRecursive param to true. Based on a file/directory list function I saw somewhere on this site. ;)
function mostRecentModifiedFileTime($dirName,$doRecursive) {
  $d = dir($dirName);
  $lastModified = 0;
  while($entry = $d->read()) {
    if ($entry != "." && $entry != "..") {
      if (!is_dir($dirName."/".$entry)) {
        $currentModified = filemtime($dirName."/".$entry);
      } else if ($doRecursive && is_dir($dirName."/".$entry)) {
        $currentModified = mostRecentModifiedFileTime($dirName."/".$entry,true);
      }
      if ($currentModified > $lastModified){
        $lastModified = $currentModified;
      }
    }
  }
  $d->close();
  return $lastModified;
}
While testing on Windows, I noticed that the precision of filemtime is just 1 second.
So if you use clearstatcache() and filemtime() to check if a file has been modified, it might fail to detect the change. The modifications just have to happen within less than a second.
(I ran into this with Apache on Windows XP.)
Here is a small but handy script that you can use to find which files in your server are modified after a date/time that you specify. This script will go through all folders in the specified directory recursively and echo the modified files with the last modified date/time...
//Starts Here
//Put here the directory you want to search for. Put / if you want to search your entire domain
$dir='/';
//Put the date you want to compare with in the format of: YYYY-mm-dd hh:mm:ss
$comparedatestr="2006-08-12 00:00:00";
$comparedate=strtotime($comparedatestr);
//I run the function here to start the search. 
directory_tree($dir,$comparedate);
//This is the function which is doing the search...
function directory_tree($address,$comparedate){ 
 @$dir = opendir($address); 
 if(!$dir){ return 0; } 
    while($entry = readdir($dir)){ 
        if(is_dir("$address/$entry") && ($entry != ".." && $entry != ".")){               
            directory_tree("$address/$entry",$comparedate);
        } 
         else  {
         if($entry != ".." && $entry != ".") {
         
          $fulldir=$address.'/'.$entry;
          $last_modified = filemtime($fulldir);
          $last_modified_str= date("Y-m-d h:i:s", $last_modified);
            if($comparedate '.$last_modified_str;
             echo "
"; } } } } }
A comment below states
 "When using this function to get the modified date of a directory, 
  it returns the date of the file in that directory that was last modified."
this is not (necessarily) correct, the modification time of a directory will be the time of the last file *creation* in a directory (and not in it's sub directories).
The above code works fine if you place it on each page you want a date stamp on. I've found that if you place a reference such as filemtime(__FILE__) in an included or required file, that the modification time of the inherited file will be returned, not the time of the file that did the ineriting.
Here's a handy little function for smart cache overriding :)

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)