How can PHP be used to create a script that organizes images based on their timestamps, as described in the forum thread?

To organize images based on their timestamps, we can use PHP to read the timestamp of each image file and then move or copy them to different directories based on their timestamps. This can be achieved by looping through the images in a directory, extracting their timestamps, and then creating new directories based on the timestamps to organize the images accordingly.

<?php
$sourceDir = 'path/to/source/directory';
$targetDir = 'path/to/target/directory';

$images = glob($sourceDir . '/*.jpg'); // assuming images are in JPG format

foreach ($images as $image) {
    $timestamp = filemtime($image);
    $newDir = date('Y-m', $timestamp); // create directory based on year and month
    $newPath = $targetDir . '/' . $newDir . '/' . basename($image);

    if (!file_exists($targetDir . '/' . $newDir)) {
        mkdir($targetDir . '/' . $newDir, 0777, true);
    }

    copy($image, $newPath); // move or copy the image to the new directory
}
?>