touch

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

touch設定文件的訪問和修改時間

說明

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

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

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

參數

filename

要設定的文件名。

mtime

要設定的時間。如果參數 mtimenull,則會使用當前系統(tǒng)的 time()。

atime

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

更新日志

版本 說明
8.0.0 mtimeatime 現在可以為空。

返回值

成功時返回 true, 或者在失敗時返回 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 參數的 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)對時間的判斷方法可能是不相同的。