Search code examples
phpeventsdatefileflat

Delete file with past date in name in PHP


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.


Solution

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