在 PHP 中删除名称中包含过去日期的文件

发布于 2024-10-14 18:49:25 字数 245 浏览 8 评论 0原文

我正在制作一个事件管理器,这几乎完成了,但有一件事。我有一个文件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

童话 2024-10-21 18:49:25

或者,更好的选择是存储数字时间戳而不是文本格式。然后,您可以使用以下命令删除必要的文件:

$dir = new DirectoryIterator('/path/to/your/files');
foreach($dir as $file) {
    ... extract timestamp from each file ...
    ... if it's less than your cutoff, delete file ...
}

这样就可以省去每次将文件名的日期部分解析为日期时间值的麻烦,并且您只需进行简单的数字比较即可。

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:

$dir = new DirectoryIterator('/path/to/your/files');
foreach($dir as $file) {
    ... extract timestamp from each file ...
    ... if it's less than your cutoff, delete file ...
}

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.

浅暮の光 2024-10-21 18:49:25

您已经使用此命名方案为您的背部制作了一根杆(文件中尽可能包含空格通常也是不好的做法),但没关系。

您需要做的是:

  1. 使用 scandir 扫描目标目录 或类似的。 (glob 可能是更好的选择,具体取决于目录的内容。 )

  2. 通过 strtotime 使用 str_replace< 后/a> 去掉“.html”部分。

  3. 取消链接文件(如果是过去的文件)。

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:

  1. Scan the target directory using scandir or similar. (glob might be a better bet depending on the contents of the directory.)

  2. 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.

  3. unlink the file if it's in the past.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文