Home  >  Article  >  Backend Development  >  PHP file operation function example: file last modification time

PHP file operation function example: file last modification time

PHPz
PHPzOriginal
2023-06-21 11:43:021308browse

PHP is a widely used server-side programming language. It has powerful file operation capabilities, and the final modification time of a file is also a common requirement for file operations. Therefore, in this article, we will explore an example of PHP file operation function-how to get the last modification time of a file.

  1. Using the filemtime() function

PHP provides a built-in function called filemtime(), which is used to return the last modification timestamp of a file. The timestamp is the number of seconds since 00:00:00, January 1, 1970, UNIX epoch time, so it is an integer value.

The following is a sample code that uses the filemtime() function to obtain the last modification time of a file:

$filename = 'example.txt';
if (file_exists($filename)) {
    echo "文件最后修改时间:" . date("Y-m-d H:i:s.", filemtime($filename));
} else {
    echo "文件不存在";
}

In the above code, first determine whether the specified file exists. If it exists, use the filemtime() function to obtain the last modification time of the file, and use the date() function to format the output time. In the output string, use dot notation to remove the decimal part of the timestamp so that it becomes an integer.

  1. Use the stat() function

Another way to get the last modification time of a file is to use the stat() function. The function returns an array containing details about the file, including file type, modification time, access time, etc.

The following is an example code for using the stat() function to obtain the last modification time of a file:

$filename = 'example.txt';
if (file_exists($filename)) {
    $stat = stat($filename);
    echo "文件最后修改时间:" . date("Y-m-d H:i:s", $stat['mtime']);
} else {
    echo "文件不存在";
}

In the above code, first determine whether the specified file exists. If it exists, use the stat() function to obtain the file information array and obtain the last modification time from the array. Finally, use the date() function to format the output time.

  1. Conclusion

Through the introduction of this article, I believe you already understand how to get the last modification time of a file in PHP. This function can be implemented very conveniently using the built-in function filemtime() or stat(). It should be noted that before using these functions, you should first determine whether the corresponding file exists. If the file does not exist, the file information cannot be obtained and an exception will be thrown at runtime.

The above is the detailed content of PHP file operation function example: file last modification time. For more information, please follow other related articles on the PHP Chinese website!

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