Home  >  Article  >  Backend Development  >  Check file modification time with PHP

Check file modification time with PHP

WBOY
WBOYOriginal
2016-07-29 09:01:041562browse

System files:

In development, we sometimes need to know the last access time and last modification time of a file. Let’s introduce the three functions provided by PHP to determine the access, creation and last modification time of a file: fileatime() , filectime() and filemtime().
1. fileatime()
int fileatime(string filename): The fileatime() function returns the last access time of filename. The last access here refers to whenever a data block of a file is read, using UNIXtime Stamp format, returns FALSE if there is an error.
2. filectime()
int filectime(string filename): The filectime() function returns the last change time of filename. The last change here refers to the last change time of inode of the specified file filename, where inode (index node) is used to store basic information of files and directories including time, file name, user and group, etc. It adopts UNIX timestamp format and returns FALSE when there is an error.
3. filemtime()
int filemtime(string filename): The filemtime() function returns the last modification time of filename. The last modification refers to the change of the content of the file, using the UNIX timestamp format. Returns FALSE if there is an error.
For example:

<code><span><span><?php</span><span>$file</span>=<span>"/software/test.txt"</span>; 
<span>echo</span><span>"文件最后访问的时间是"</span>.date(<span>"Y-m-d H:i:s"</span>,fileatime(<span>$file</span>)).<span>"<br/>"</span>; 
<span>echo</span><span>"文件最后改变的时间是"</span>.date(<span>"Y-m-d H:i:s"</span>,filectime(<span>$file</span>)).<span>"<br/>"</span>; 
<span>echo</span><span>"文件最后修改的时间是"</span>.date(<span>"Y-m-d H:i:s"</span>,filemtime(<span>$file</span>)).<span>"<br/>"</span>; 
<span>?></span></span></code>

Remote files

<code><span><span>function</span><span>remote_filectime</span><span>(<span>$url_file</span>)</span>{</span><span>$headInf</span> = get_headers(<span>$url_file</span>,<span>1</span>);  <span>//注意第二个参数 </span><span>return</span> strtotime(<span>$headInf</span>[<span>'Last-Modified'</span>]);   
} </code>

get_headers return data

<code><span>Array</span>
(
    [<span>0</span>] => HTTP/<span>1.1</span><span>200</span> OK
    [Server] => nginx
    [Date] => Wed, <span>02</span> Mar <span>2016</span><span>07</span>:<span>34</span>:<span>52</span> GMT
    [Content-Type] => text/xml
    [Content-Length] => <span>2750</span>
    [Connection] => close
    [Set-Cookie] => IPLOC=CN1100; expires=Thu, <span>02</span>-Mar-<span>17</span><span>07</span>:<span>34</span>:<span>52</span> GMT; path=/
    [P3P] => CP=<span>"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"</span>
    [ETag] => <span>"Ahh8eNBCjmL"</span>
    [Last-Modified] => Tue, <span>02</span> Feb <span>2016</span><span>09</span>:<span>55</span>:<span>40</span> GMT
    [Accept-Ranges] => bytes
)</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces PHP to check the file modification time, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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