How can PHP be used to automatically delete images older than a certain timeframe on a server?
To automatically delete images older than a certain timeframe on a server using PHP, you can create a script that checks the creation or modification date of each image file in a specified directory. If the image file is older than the specified timeframe, it can be deleted using the `unlink()` function in PHP.
<?php
$directory = '/path/to/image/directory/';
$threshold = strtotime('-1 week'); // Delete images older than 1 week
$files = scandir($directory);
foreach ($files as $file) {
if (is_file($directory . $file)) {
$fileCreationTime = filectime($directory . $file);
if ($fileCreationTime < $threshold) {
unlink($directory . $file);
echo 'Deleted ' . $file . PHP_EOL;
}
}
}
?>