PHP 中的 touch() 函数

phpprogrammingserver side programming更新于 2025/9/19 10:52:17

touch() 函数用于设置文件的访问和修改时间。成功时返回 TRUE,失败时返回 FALSE。

语法

touch(filename, time, atime)

参数

  • filename − 设置文件名。

  • time − 设置时间。默认为当前系统时间。

  • atime − 设置访问时间。默认值为当前系统时间。

返回

touch() 函数成功时返回 TRUE,失败时返回 FALSE。

示例

<?php
$myfile = "new.txt";
// 将修改时间更改为当前系统时间
if (touch($myfile)) {
   echo ("$myfile 的修改时间设置为当前时间。");
} else {
   echo ("$myfile 的修改时间无法更新。");
}
?>

输出

new.txt 的修改时间设置为当前时间。

让我们看看另一个例子。

示例

<?php
$myfile = "new.txt";
$set_time = time() - 28800;
// 更改修改时间
if (touch($myfile, $set_time)) {
   echo ("$myfile 的修改时间已更新为 8 小时前。");
} else {
   echo ("$myfile 的修改时间无法更新。");
}
?>

输出

new.txt 的修改时间已更新为 8 小时前。

相关文章