touch

(PHP 4, PHP 5, PHP 7, PHP 8)

touch設(shè)定文件的訪問和修改時(shí)間

說明

touch(
    string $filename,
    int $time = time(),
    int $atime = ?,
    ?int $mtime = null,
    ?int $atime = null
): bool

嘗試將由 filename 給出的文件的訪問和修改時(shí)間設(shè)定為給出的 mtime。 注意訪問時(shí)間總是會(huì)被修改的,不論有幾個(gè)參數(shù)。

如果文件不存在,則會(huì)被創(chuàng)建。

參數(shù)

filename

要設(shè)定的文件名。

mtime

要設(shè)定的時(shí)間。如果參數(shù) mtimenull,則會(huì)使用當(dāng)前系統(tǒng)的 time()。

atime

如果這個(gè)參數(shù)不是 null,則給定文件的訪問時(shí)間會(huì)被設(shè)為 atime。 否則會(huì)設(shè)置為 mtime。 如果兩個(gè)參數(shù)都是 null,則使用當(dāng)前系統(tǒng)時(shí)間。

更新日志

版本 說明
8.0.0 mtimeatime 現(xiàn)在可以為空。

返回值

成功時(shí)返回 true, 或者在失敗時(shí)返回 false

范例

示例 #1 touch() 例子

<?php
if (touch($filename)) {
    echo 
$filename ' modification time has been changed to present time';
} else {
    echo 
'Sorry, could not change modification time of ' $filename;
}
?>

示例 #2 使用 mtime 參數(shù)的 touch()

<?php
// This is the touch time, we'll set it to one hour in the past.
$time time() - 3600;

// Touch the file
if (!touch('some_file.txt'$time)) {
    echo 
'Whoops, something went wrong...';
} else {
    echo 
'Touched file with success';
}
?>

注釋

注意:

注意:不同文件系統(tǒng)對(duì)時(shí)間的判斷方法可能是不相同的。