如何在不知道文件扩展名的情况下通过名称取消链接()?
简而言之
我们有一个名为clients.(唯一参数)
的文件。现在我们想要 unlink()
它,但由于我们不知道文件扩展名,我们如何才能成功呢?
更长的故事
我有一个缓存系统,其中 md5()
中的数据库查询是文件名,缓存到期日期是扩展名。
示例: 896794414217d16423c6904d13e3b16d.3600
但有时过期日期会发生变化。因此,对于最终的解决方案,应该忽略文件扩展名。
我能想到的唯一方法是搜索目录并匹配文件名,然后获取文件扩展名。
In short
We have a a file called clients.(unique parameter)
. And now we want to unlink()
it, but as we don't know the file extension, how do we succeed?
Longer story
I have a cache system, where the DB query in md5()
is the filename and the cache expiration date is the extension.
Example: 896794414217d16423c6904d13e3b16d.3600
But sometimes the expiration dates change. So for the ultimate solution, the file extension should be ignored.
The only way I could think of is to search the directory and match the filenames, then get the file extension.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
glob()
:如果需要,您可以例如,可以检查
glob()
返回的每个文件的filemtime()
对它们进行排序,以便只删除最旧的文件。Use a
glob()
:If you need to, you can check the
filemtime()
of each file returned by theglob()
to sort them so that you only delete the oldest, for example.您在目录中搜索匹配文件名的猜测是正确的。您可以采取多种方法:
readdir
相关文件夹glob
按照 Micheal 的建议您还可以获得
ls {$target_dir} | 的输出grep {$file_first_part}
然后取消链接结果字符串(假设找到匹配项)。You are correct in your guess to search the directory for a matching file name. There are multiple approaches you could take:
readdir
the folder in questionglob
as suggested by MichealYou could also get the output of
ls {$target_dir} | grep {$file_first_part}
and then unlink the resulting string (assuming a match is found).