在 PHP 中删除名称中包含过去日期的文件
我正在制作一个事件管理器,这几乎完成了,但有一件事。我有一个文件 view.php
用于查看事件。但是,我需要删除过去日期的事件。
它是平面文件,因此日期以文件名形式存储在 data
文件夹中。他们的名字就是日期。例如:"data/Monday, 21 February, 2011.html"
就是该日期。有人对如何删除过去的事件(文件)有任何想法吗?
提前致谢。
I have an event manager i am making, thats almost done, but one thing. I have a file, view.php
that view the events. however, i need to delete events that have dates in the past.
it is flat file, so the dates are stores as file names, in the folder data
. their names would be dates. for instance: "data/Monday, 21 February, 2011.html"
would be for that date. does anybody have any ideas on how to delete events (files) that are in the past?
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者,更好的选择是存储数字时间戳而不是文本格式。然后,您可以使用以下命令删除必要的文件:
这样就可以省去每次将文件名的日期部分解析为日期时间值的麻烦,并且您只需进行简单的数字比较即可。
Or, a better option would be to store a numeric timestamp instead of the text format. Then you could delete the necessary files with something like:
That'd save you the trouble of having to parse the date part of the filename into a datetime value each time, and you're down to a simple numeric comparison.
您已经使用此命名方案为您的背部制作了一根杆(文件中尽可能包含空格通常也是不好的做法),但没关系。
您需要做的是:
使用 scandir 扫描目标目录 或类似的。 (glob 可能是更好的选择,具体取决于目录的内容。 )
通过 strtotime 使用 str_replace< 后/a> 去掉“.html”部分。
取消链接文件(如果是过去的文件)。
You've made a rod for your back with this naming scheme (it's also generally bad practice to have files with spaces in as far as possible), but no matter.
What you need to do is:
Scan the target directory using scandir or similar. (glob might be a better bet depending on the contents of the directory.)
Convert each valid (i.e.: not "." or "..") html file name into a timestamp via strtotime after using str_replace to get rid of the ".html" portion.
unlink the file if it's in the past.