<body style="background: gray;">
<pre>
<?php
date_default_timezone_set('Europe/Warsaw'); // https://www.php.net/manual/en/timezones.europe.php
const unlinked_folder = './new_folder_to_unlink/'; // slash at the end mean folder, without slash mean file
if(file_exists(unlinked_folder)) {
$date1 = new DateTime(date("Y-m-d H:i:s", stat(unlinked_folder)[10]));
$date2 = new DateTime();
// "2020-01-28 13:10:00"
$interval = $date1->diff($date2);
} else {
$interval = false;
}
//echo $interval->format("%Y-%M-%D %H:%I:%S %a");
const day_passed = 0;
if($interval !== false && (int)$interval->format("%a") == day_passed) {
//const hello = 200;
//CONST deleted_file = 200;
if(is_file(unlinked_folder)) {
$deleted_file = unlink(unlinked_folder);
} else {
$deleted_file = rmdir(unlinked_folder);
}
printf("Your file %s was %sdeleted successfully.", unlinked_folder, $deleted_file === true ? '' : 'no ');
} else if($interval !== false) {
printf("Your file %s is still before date %s days to unlink.", unlinked_folder, (string)day_passed);
} else {
echo 'File not found.';
}
?>
</pre>
</body>
Pod stałą unlinked_folder podstawiasz nazwę swojego folderu lub pliku. Folder kończy się na końcu slashem "/", natomiast plik, nie. Jest tak dlatego, że plik może być chyba bez rozszerzenia i gdy nie ma slasha na końcu, to nie wiadomo, czy to plik, czy folder.
Stała day_passed natomiast, to liczba dni upłynięcia od daty utworzonego pliku/folderu. Umieściłem tam 0, znaczy, to że jeśli plik został utworzony dzisiaj, to 0 oznacza usuń plik/folder, jeśli został utworzony dzisiaj. Gdy wpiszesz tam inną liczbę np. 4, to wtedy plik/folder zostanie usunięty po 4 dniach.