Home  >  Article  >  Backend Development  >  Detailed explanation of how to use php to change the time attribute of a file

Detailed explanation of how to use php to change the time attribute of a file

PHPz
PHPzOriginal
2023-04-03 15:01:001148browse

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.';
}
# The ##touch() function can also change the access time and modification time of a file, and can also be used to create files. Its syntax is as follows:

bool touch ( string $filename [, int $time = time() [, int $atime ]] )
Among them, the $filename parameter is the time to be changed or the name of the file to be created, the $time parameter is optional and is used to set the modification time, and the $atime parameter is optional and is used to set access time. If you do not specify the $time and $atime parameters, the touch() function will set the current time for the file.

For example, the following sample code will change the access and modification time of the test.txt file and create a new file new.txt and set the access and modification time to the current time:

// 更改文件时间
$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.';
}
In the above example, we can see that PHP provides two functions for changing file time. They are utime() and touch() functions. Developers can choose which function to use according to their own needs.

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!

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