Skip to content

CHICPRO

  • Life Log
  • Cycling Log
  • Photo Log
    • Portrait
    • Landscape
    • Flower
    • Etc
  • Coding Log
  • Information

[PHP] 일정기간 지난 파일 삭제

2013-02-21 by 편리

특정 디렉토리 내의 모든 파일을 체크해서 최종 수정일 30일이상 경과된 파일은삭제하는 코드이다.

최종 수정 시간은 filemtime()을 이용해 구할 수 있으며 그누보드의 경우 index.php 파일 있기 때문에

이 파일은 삭제하지 않도록 했다. 그누보드 관련 작업을 하다 보니 자연스레 이렇게 된다. ^^’

<?

$dir =’./data/file’;

recursive_file_delete($dir);

function recursive_file_delete($dir)

{

    if(is_dir($dir)) {

        if($dh = opendir($dir)) {

            while(($entry = readdir($dh)) !== false) {

                if($entry == ‘.’ || $entry == ‘..’)

                    continue;

                $subdir = $dir.’/’.$entry;

                if(is_dir($subdir)) {

                    recursive_file_delete($subdir);

                } else {

                    if($entry == ‘index.php’)

                        continue;

                    $sfile = $dir.’/’.$entry;

                    $mtime = @filemtime($sfile);

                    // 최종수정일이 30일 이상인 파일만 삭제

                    if(file_exists($sfile) && (time() – $mtime <= 24*60*60*30))

                        continue;

                    // 파일삭제

                    @unlink($sfile);

                }

            }

            closedir($dh);

        }

    }

}

?>

Post navigation

Previous Post:

[PHP] 서브 디렉토리를 포함한 디렉토리의 파일리스트 구하기

Next Post:

감기가 오는 듯..

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • php 배열 연산에서 + 와 array_merge 의 차이
  • pcntl_fork 를 이용한 다중 프로세스 실행
  • 아이폰 단축어를 이용하여 주중 공휴일엔 알람 울리지 않게 하기
  • 구글 캘린더 전체일정 재동기화
  • OpenLiteSpeed 웹서버에 HTTP 인증 적용
  • OpenLiteSpeed 웹어드민 도메인 연결
  • WireGuard를 이용한 VPN 환경 구축
  • Ubuntu 22.04 서버에 OpenLiteSpeed 웹서버 세팅
  • 맥 vim 세팅
  • 우분투 시스템 터미널쉘 zsh 로 변경

Recent Comments

  • 편리 on 업무관리용 그누보드 게시판 스킨
  • 임종섭 on 업무관리용 그누보드 게시판 스킨
  • 캐논 5D 펌웨어 | Dslr 펌웨어 업그레이드 방법 82 개의 베스트 답변 on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • Top 5 캐논 5D 펌웨어 Top 89 Best Answers on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • 편리 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 임팀장 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 편리 on Notepad++ NppFTP 플러그인 수동 설치
  • paul-j on Notepad++ NppFTP 플러그인 수동 설치
  • YS on Windows 10 iCloud 사진 저장 폴더 변경
  • 편리 on Docker를 이용한 Centos7 + httpd + php 5.4 개발환경 구축

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2025 CHICPRO | Built using WordPress and SuperbThemes