开发者

Delete file with past date in name in PHP

开发者 https://www.devze.com 2023-02-07 06:40 出处:网络
I have a开发者_运维技巧n 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.

I have a开发者_运维技巧n 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消