Is using CRON a recommended method for deleting files in PHP?

Using CRON for deleting files in PHP is a common and recommended method. You can create a PHP script that deletes files and schedule it to run at specific intervals using CRON jobs. This allows you to automate the process of deleting files without manual intervention.

<?php
// Delete files older than 7 days
$dir = '/path/to/directory/';
$files = glob($dir . '*');
$now = time();

foreach ($files as $file) {
    if (is_file($file) && $now - filemtime($file) >= 7 * 24 * 60 * 60) {
        unlink($file);
    }
}
?>