What is the best way to delete HTML files of a certain age using PHP?
To delete HTML files of a certain age using PHP, you can use the `filemtime()` function to get the last modification time of the file and compare it with the current time minus the age limit. If the file is older than the specified age, you can use the `unlink()` function to delete it.
$directory = 'path/to/html/files/';
$ageLimit = 86400 * 7; // 7 days in seconds
$files = glob($directory . '*.html');
$current_time = time();
foreach ($files as $file) {
if (is_file($file) && $current_time - filemtime($file) >= $ageLimit) {
unlink($file);
}
}
Keywords
Related Questions
- What are some common pitfalls when using shorthand notation in PHP, and how can they be avoided?
- Are there any specific PHP functions or libraries that can help prevent users from logging in multiple times with the same credentials?
- What security considerations should be taken into account when troubleshooting unexpected behavior in PHP form submissions?