폴더 내 3일 지난 파일 삭제

Sangyeong Je·2023년 1월 20일
0

cron

목록 보기
1/1

임시 파일이 저장되는 폴더가있어 잡을 통해 3일 후 자동삭제처리

$tempPath = "/erp/KIC_IN/FilesTemp/";
$dir = array_diff(scandir($tempPath), array('.', '..'));

foreach ($dir as $dirName) 
{
   $dirPath = '/erp/KIC_IN/FilesTemp/'.$dirName.'/';
   $files = glob($dirPath . '*');
   $now = time();
   $threeDaysAgo = $now - (3 * 24 * 60 * 60);
   foreach ($files as $file) {
      if (is_file($file) && filemtime($file) < $threeDaysAgo)
      {
          unlink($file);
      }
  }
}
$tempPath = "/erp/KIC_IN/FilesTemp/";
= $tempPath에 기본 경로를 설정해줍니다.

$dir = array_diff(scandir($tempPath), array('.', '..'));
= scandir() 함수로 기본 경로내 모든 디렉토리의 배열을 가져오고 
array_diff() 함수를 통해 배열 scandir($tempPath)array('.','..')을 비교하여 겹치지않는값을 리턴합니다.scandir($tempPath) 배열에서 ...을 제거합니다.

foreach ($dir as $dirName) 
= 기본 경로내 디렉토리들을 조회하기 위해 반복문을 실행
{
	$dirPath = '/erp/KIC_IN/FilesTemp/'.$dirName.'/';
    = 기본 경로내 디렉토리들을 $dirPath에 할당합니다.
    $files = glob($dirPath . '*');
    = $dirPath내 모든 파일들의 배열을 $files에 할당합니다.
   	$now = time();
    $threeDaysAgo = $now - (3 * 24 * 60 * 60);
    = 3일 전을 구하기위해 $now에 현재시간을 할당, $threeDaysAgo-3일을 할당한다.
    foreach ($files as $file) {
    = 디렉토리내 파일들의 최종수정날짜를 비교하기위해 반복문을 실행
        if (is_file($file) && filemtime($file) < $threeDaysAgo)
        = $fileis_file() 함수를 통해 디렉토리가 아닌 파일인지
        = $filefilemtime() 함수를 통해 최종 수정일이 3일이 지났는지 확인합니다.
        {
            unlink($file);
            = $file에 파일이고 수정된지 3일이지났으면 unlink()함수를 통해 삭제합니다.
        }
    }
}



0개의 댓글