gpt4 book ai didi

PHP 检查符号链接(symbolic link)文件的年龄

转载 作者:搜寻专家 更新时间:2023-10-31 21:27:02 26 4
gpt4 key购买 nike

我有一个下载脚本,它使用 symlink() 创建一个文件链接。我正在尝试创建一个脚本来删除链接 1 小时后的链接。但是,当我尝试使用 filemtime() 检查文件的年龄时,我只得到实际原始(目标)文件的修改时间,而不是符号链接(symbolic link)。

这是我当前的代码:

$filename = '/var/www/html/files/myfile.rar';

if (file_exists($filename)) {
echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}

我应该怎么做才能检查实际符号链接(symbolic link)而不是文件的最后修改时间?

最佳答案

正确,filemtime()stat()将遵循符号链接(symbolic link),但是 lstat()不会的。

This function is identical to the stat() function except that if the filename parameter is a symbolic link, the status of the symbolic link is returned, not the status of the file pointed to by the symbolic link.

例如:

<?php

function getSymlinkMtime($symlinkPath)
{
$stat = lstat($symlinkPath);

return isset($stat['mtime']) ? $stat['mtime'] : null;
}

可以用来代替 filemtime()

在这种方法中,您的完整示例是:

<?php

$filename = '/var/www/html/files/myfile.rar';

if (file_exists($filename)) {
echo "$filename was last modified: " . date ("F d Y H:i:s.", getSymlinkMtime($filename));
}

关于PHP 检查符号链接(symbolic link)文件的年龄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34512105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com