简体   繁体   中英

Delete a file if the timestamp is more than 20 minutes php

I am trying to delete a $lockfile if the timestamp is more than 20 minutes.

if (file_exists($lockfile) && time() - filemtime($lockfile) > strtotime("+20 minutes")) {
    // If lockfile is alive for more than 20 minutes, unlink it
    unlink($lockfile);
}

I can not figure out why it is not working. Probably something simple that I am overlooking right now. Thank you in advance!

strtotime("+20 minutes") will return the timestamp of the date in 20 minutes from now, which is larger that the difference of the two timestamps. You should replace it by the time 20 minutes take in seconds, so:

if (file_exists($lockfile) && time() - filemtime($lockfile) > 20*60) {
    // If lockfile is alive for more than 20 minutes, unlink it
    unlink($lockfile);
}

That should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM