How can PHP scripts be used to automatically delete files from a specific folder after a set period of time?
To automatically delete files from a specific folder after a set period of time, you can create a PHP script that checks the creation or modification date of each file in the folder and deletes files that are older than the specified time period. This can be achieved by using functions like `scandir()` to get a list of files in the folder, `filemtime()` to get the last modification time of a file, and `unlink()` to delete files.
<?php
$folder = 'path/to/folder'; // Specify the folder path
$files = scandir($folder);
foreach ($files as $file) {
if (is_file($folder . '/' . $file)) {
$fileTime = filemtime($folder . '/' . $file);
$currentTime = time();
$expirationTime = $currentTime - (24 * 60 * 60); // Set expiration time to 24 hours
if ($fileTime < $expirationTime) {
unlink($folder . '/' . $file);
echo "Deleted: " . $file . "\n";
}
}
}
?>