百科狗-知识改变命运!
--

touch() - 设定文件的访问和修改时间 - php 文件目录函数

百变鹏仔1年前 (2023-11-21)阅读数 20#技术干货
文章标签时间

touch()

(PHP 4, PHP 5, PHP 7)

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

说明

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

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

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

参数

$filename

要设定的文件名。

$time

touch() - 设定文件的访问和修改时间 - php 文件目录函数

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

$atime

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

返回值

成功时返回TRUE,或者在失败时返回FALSE

更新日志

版本说明
5.3.0能够修改 Windows 下目录的最后修改时间。

范例

Example #1touch()例子

使用$time参数的touch()

Average time: 0,1145s

Average time: 0,2322s
So, file_put_contents is faster than touch, about two times.
Only way to change modification date in catalogue is to create file in via touch() and dalete it with unlink(): 
Note: the script to touch a file you don't own will change it's owner so ensure permissions are correct or you could lose access to it
Actually, Glen is right, PHP won't touch if it is not the current owner of the file, even if the directory and files are writeable by the PHP user.
To touch a file without being owner, it is much easier: 
At least on Linux, touch will not change the time on a symlink itself, but on the file/directory it points to. The only way to work around this is to unlink the symlink, then recreate it.
It took a bit of searching to discover this. The OS itself provides no way to do it. Many people wondered why anyone would want to do this. I use symlinks inside a web tree to point to files outside the web tree. After a certain length of time has passed, I want the symlinks to die, so the files cannot be successfully hotlinked.
I needed to use this to touch the /etc/cron.d directory when I updated some files in there. I know the docs say this isn't necessary, but I'm finding that i need to do it in order form my changes to be picked up quickly. 
I ran into the permissions error as well and I found that using chmod 777 /etc/cron.d does the trick.
So, you should be able to use the PHP touch function on a directory that has open write access.
Of course, this isn't the most secure approach, but in our application it's not a big deal for that folder to not be super secure.
A better explanation:
For file $file and UNIX time stored in vars $access and $modified
- change only access time
\touch($file, \filemtime($file), $access);
- change only modified time
\touch($file, $modified, \fileatime($file));
- change both access and modified time
\touch($file, $modified, $access);
Seeing the results:
//use a session cookie stored in a custom folder
$file = '/var/www/test_com/session/sess_qfn587cudfpgsijm1bs4d81s75';
echo 'stats for sess_qfn587cudfpgsijm1bs4d81s75
'; \clearstatcache(); echo 'access: '.\date("Y-m-d H:i:s", \fileatime($file)).'
'; echo 'modified: '.\date("Y-m-d H:i:s", \filemtime($file)).'
'; echo 'change access to now, modified +1 hour
'; \touch($x, \filemtime($file)+3600, time()); \clearstatcache(); echo 'access: '.\date("Y-m-d H:i:s", \fileatime($file)).'
'; echo 'modified: '.\date("Y-m-d H:i:s", \filemtime($file)).'
'; Notice the double call to clearstatcache()!
In unix on the command-line, you can touch files you don't own - but like other comments on this page state - PHP's built in touch won't work.
I simple alternative (on unix): 
Important info:
touch() used on a directory always returns FALSE and prints "Permission denied" on NTFS and FAT Filesystem (tested on winXP).
Here's a little workaround that allows the PHP user to touch a file it doesn't own:

Of course, PHP needs to have write access to the folder containing the file you want to touch, but that should be easy to arrange.
Neat little script that will give you a list of all modified files in a certain folder after a certain date:
$filelist = Array();
$filelist = list_dir("d:\\my_folder");
for($i=0;$i 2001) && ($test[1] > 16) && ($test[0] > 5)){
    echo $filelist[$i]."\r\n";
  }
  clearstatcache();
}
function list_dir($dn){
  if($dn[strlen($dn)-1] != '\\') $dn.='\\';
  static $ra = array();
  $handle = opendir($dn);
  while($fn = readdir($handle)){
    if($fn == '.' || $fn == '..') continue;
    if(is_dir($dn.$fn)) list_dir($dn.$fn.'\\');
    else $ra[] = $dn.$fn;
  }
  closedir($handle);
  return $ra;
}
An earlier comment referenced a code snippet that showed file_put_contents() was faster the touch for creating files. I re-ran the same tests on PHP 5.5.9 and this seems to no longer be the case. 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)