PHP is an open source server-side programming language commonly used for web development. In PHP, we can use built-in functions to change the file time.
Under Linux/Unix systems, each file has three time attributes, namely access time, modification time and status change time. In PHP, you can use the utime() and touch() functions to change the access time and modification time of a file.
utime() function is used to change the access time and modification time of the file. Its syntax is as follows:
bool utime ( string $filename , int $time )
Among them, the $filename parameter is the file name to change the time, and the $time parameter is the timestamp. If you need to set both the access time and modification time of the file to the current time, you can write like this:
utime($filename, time());
For example, the following sample code will change the access and modification time of the test.txt file:
$filename = 'test.txt'; if(file_exists($filename)) { utime($filename, time()); echo 'File time changed.'; } else { echo 'File not exists.'; }
bool touch ( string $filename [, int $time = time() [, int $atime ]] )
// 更改文件时间 $filename = 'test.txt'; if(file_exists($filename)) { touch($filename); echo 'File time changed.'; } else { echo 'File not exists.'; } // 创建新文件并设置时间 $new_file = 'new.txt'; if(touch($new_file)) { echo 'New file created and time set.'; } else { echo 'Failed to create new file.'; }
The above is the detailed content of Detailed explanation of how to use php to change the time attribute of a file. For more information, please follow other related articles on the PHP Chinese website!