Home > php教程 > PHP源码 > body text

PHP判断文件是否被修改实例

WBOY
Release: 2016-06-08 17:23:26
Original
1440 people have browsed it

在网站的管理系统中,有时需要查看某个文件是否被修改过、在什么时间被修改的、最后的修改时间是什么时候,本实例就可以实现这个功能,对表单中提交的文件进行判断,检测出修改时间。


关键技术

   本实例主要应用filectime()和filemtime()函数,检测文件的incode最后改变时间和最后的修改时间,并应用date()函数对检测返回的时间戳进行格式化。
   filectime()函数,返回指定文件filename的inode最后改变时间,语法如下:
   int filectime(string filename);
   成功则返回UNIX时间戳,否则返回FALSE。
   filemtime()函数,返回指定文件filename的最后修改时间,语法如下:
   成功则返回UNIX时间戳,否则返回FALSE。
设计过程
  首先,设计网页页面。然后,创建一个表单,通过文件域提交要判断的文件。接着,获取表单中提交的文件路径,应用filectime()和filemtime()函数对提交的文件进行检测。最后,输出检测结果。
  index.php文件的关键代码如下:

 代码如下 复制代码


  $file=iconv("utf-8","gb2312",$_POST['files']);           //实现编码格式的转换
  if(file_exists($file)){                                  //判断文件是否存在
          $change_time=filectime($file);                   //获取文件的最后incode时间
          $time=date("Y-m-d h:i:s",$change_time);          //时间戳的格式化
          $last_time=filemtime($file);                     //获取文件的最后修改时间
          $times=date("Y-m-d h:i:s",$last_time);           //时间戳的格式化
   }else{
          $result="该文件不存在";
   }
   ?>


秘笈心法
  获取文件的最后访问时间。
  通过文件系统函数不但可以获取文件的最后修改时间,而且可以获取文件的最后访问时间,其应用的是fileatime()函数

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!