What are some best practices for managing and deleting old image files in a PHP script for video surveillance purposes?

When managing and deleting old image files in a PHP script for video surveillance purposes, it is important to regularly check and remove outdated images to free up storage space and maintain system performance. One approach is to set a specific time frame for how long images should be kept before being deleted. You can achieve this by using PHP to check the creation date of each image file and delete those that exceed the specified time limit.

// Set the directory path where image files are stored
$imageDirectory = "path/to/image/directory/";

// Set the time frame for how long to keep image files (in seconds)
$timeLimit = 86400; // 24 hours

// Get all image files in the directory
$files = glob($imageDirectory . "*.{jpg,jpeg,png,gif}", GLOB_BRACE);

// Loop through each file and check its creation time
foreach ($files as $file) {
    $creationTime = filectime($file);
    $currentTime = time();

    // If the file is older than the time limit, delete it
    if (($currentTime - $creationTime) > $timeLimit) {
        unlink($file);
    }
}