How can PHP be used to automatically sort and organize images based on date and time?
To automatically sort and organize images based on date and time using PHP, you can use the `glob()` function to retrieve a list of image files in a directory, then use `filemtime()` to get the last modified timestamp of each file. Finally, you can sort the files based on their timestamps and move them to the appropriate folders.
$directory = 'path/to/images/';
$files = glob($directory . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
foreach ($files as $file) {
$timestamp = filemtime($file);
$date = date('Y-m-d', $timestamp);
$time = date('H-i-s', $timestamp);
$newDirectory = $directory . $date . '/';
if (!file_exists($newDirectory)) {
mkdir($newDirectory, 0777, true);
}
rename($file, $newDirectory . $time . '_' . basename($file));
}