Подтвердить что ты не робот

PHP скрипт для удаления файлов старше 24 часов, удаляет все файлы

Я написал этот PHP скрипт для удаления старых файлов старше 24 часов, но он удалил все файлы, включая более новые:

<?php
  $path = 'ftmp/';
  if ($handle = opendir($path)) {
     while (false !== ($file = readdir($handle))) {
        if ((time()-filectime($path.$file)) < 86400) {  
           if (preg_match('/\.pdf$/i', $file)) {
              unlink($path.$file);
           }
        }
     }
   }
?>
4b9b3361

Ответ 1

(time()-filectime($path.$file)) < 86400

Если текущее время и время изменения файла находятся в пределах 86400 секунд друг от друга, тогда...

 if (preg_match('/\.pdf$/i', $file)) {
     unlink($path.$file);
 }

Думаю, это может быть твоя проблема. Измените его нa > или >= и он должен работать правильно.

Ответ 2

<?php

/** define the directory **/
$dir = "images/temp/";

/*** cycle through all files in the directory ***/
foreach (glob($dir."*") as $file) {

/*** if file is 24 hours (86400 seconds) old then delete it ***/
if(time() - filectime($file) > 86400){
    unlink($file);
    }
}

?>

Вы также можете указать тип файла, добавив расширение после * (подстановочный знак), например

Для изображений jpg используйте: glob($dir."*.jpg")

Для файлов txt используйте: glob($dir."*.txt")

Для файлов htm используйте: glob($dir."*.htm")

Ответ 3

  • Вместо этого вы хотите >.
  • Если вы не работаете в Windows, вам нужно filemtime().

Ответ 4

<?php   
$dir = getcwd()."/temp/";//dir absolute path
$interval = strtotime('-24 hours');//files older than 24hours

foreach (glob($dir."*") as $file) 
    //delete if older
    if (filemtime($file) <= $interval ) unlink($file);?>

Ответ 5

работает нормально

$path = dirname(__FILE__);
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
$timer = 300;
$filetime = filectime($file)+$timer;
$time = time();
$count = $time-$filetime;
    if($count >= 0) {
      if (preg_match('/\.png$/i', $file)) {
        unlink($path.'/'.$file);
      }
    }
}
}

Ответ 6

$path = '/cache/';
// 86400 = 1day

if ($handle = opendir($path)) {
     while (false !== ($file = readdir($handle))) {
        if ( (integer)(time()-filemtime($path.$file)) > 86400 && $file !== '.' && $file !== '..') {
                unlink($path.$file);
                echo "\r\n the file deleted successfully: " . $path.$file;
        } 
     }
}

Ответ 7

привет, кто-нибудь, напишите этот скрипт на моем сайте, его удаление автоматически, пожалуйста, помогите мне удалить его

его удаление idex файла с хостинга