How can PHP be used to filter and move files based on creation or modification dates, especially in the context of a single file gallery?

To filter and move files based on creation or modification dates in a single file gallery, you can use PHP to iterate through the files in the gallery directory, retrieve their creation or modification dates, and then move them to a different directory based on specified criteria. This can help organize the gallery by sorting files according to their dates.

<?php
$sourceDir = 'gallery/';
$destinationDir = 'sorted_gallery/';

$files = scandir($sourceDir);

foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
        $filePath = $sourceDir . $file;
        $fileCreationDate = filectime($filePath); // Get creation date
        // $fileModifiedDate = filemtime($filePath); // Get modification date

        // Move files older than a certain date to a different directory
        if ($fileCreationDate < strtotime('2022-01-01')) {
            rename($filePath, $destinationDir . $file);
        }
    }
}
?>