What are the potential pitfalls of using PHP to delete files based on their age and extension?

One potential pitfall of using PHP to delete files based on their age and extension is the risk of accidentally deleting important files if the code is not properly tested or if there are errors in the logic. To mitigate this risk, it is important to thoroughly test the code and ensure that it only targets the specific files intended for deletion based on the specified criteria.

<?php
$directory = '/path/to/directory/';
$extension = 'txt';
$age = 30; // 30 days

$files = glob($directory . '*.' . $extension);
foreach ($files as $file) {
    if (is_file($file) && filemtime($file) < strtotime("-$age days")) {
        unlink($file);
    }
}
?>