It is necessary to get the time when the file is accessed and modified. In the section, you learned how to access and modification Time in PHP.
Here I am going to cover one quick article about three file functions fileatime( ) , filemtime( ), and filectime( ) which returns the time of last access, modification, and metadata change of a file respectively.
What is fileatime()?
fileatime() function gives the access time of the file is updated when you open a file or when it is used for other operations.
1 2 3 | $lastAccess = fileatime('test.php'); |
What is filemtime()?
filemtime() function gives the modification time of the file when you change the contents or save the file.
1 2 3 | $lastModification = filemtime('test.php'); |
What is filectime()?
filectime() function gives the time of file when change file attributes, such as file owner, permissions, or moved to another file system. It is also update and change the file modification time.
1 2 3 | $lastChange = filectime('test.php'); |
We can use one more file function touch()
which sets the access and modification time of file.We can say touch() function returns file touch time.
SYNTAX:
1 2 3 |
Here, First parameter filename specifies the name of the file.second parameter is the modification time that will change. The time parameter is directly responsible for the prescribed time. The time parameter is optional so you can ignore it you want to set the current time.
Let’s have a look of touch function used as:
1 2 3 4 | touch('test.php'); // set modification time to now touch('test.php',$timestamp); // set modification time to $timestamp |
The second parameter in touch() function is optional and without the second parameter, touch() set current date and time of modification of a file.To set a modification time of file with the specific value, pass that value as a timestamp to touch( ) function as a second parameter.
Now Let’s get some basic idea about fileatime( ) , filemtime( ), and filectime( ) functions:
The fileatime( ) function returns the last time a file was opened for reading or writing.
The filemtime( ) function returns the last time the contents of files were changed.
The filectime( ) function returns the last time a contents or metadata like an owner or permissions of files were changed.
NOTE: Each function gives the time as an timestamp.
1 2 3 |
Above example will gives the actual last modification time of File.
OUTPUT:
1 2 3 | Last Modified Time of FILE: 03/04/12 21:32:18 |
Also Read:
Method Chaining in PHP
To Unzip file and move to destination in PHP