touch 设定文件的访问和修改时间

发表日期:2021-07-01 08:55:45 | 来源: | | 浏览(665) 分类:文件系统函数

touch

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

touch设定文件的访问和修改时间

说明

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

尝试将由 filename   给出的文件的访问和修改时间设定为给出的 time。   注意访问时间总是会被修改的,不论有几个参数。

如果文件不存在,则会被创建。

参数


  • filename

  • 要设定的文件名。

  • time

  • 要设定的时间。如果没有提供参数 time 则会使用当前系统的时间。

  • atime

  • 如果给出了这个参数,则给定文件的访问时间会被设为   atime,否则会设置 为time。如果没有给出这两个参数,则使用当前系统时间。


返回值

成功时返回 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 使用 time 参数的 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';
}
?>


注释

注意:

注意:不同文件系统对时间的判断方法可能是不相同的。


警告  

在 PHP 5.3.0 之前无法修改 Windows 下目录的最后修改时间。

集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。