#!/home1/ysoftman/local/php/bin/php -q
<?
// 현재 시간을 체크하여 파일 및 디렉토리 삭제
function DelDir($dir)
{
/// 현재 시간 알기
$CurTime = date("Y-m-d-H-i-s", time());
// 년 월 일 시 분 초 추출
$temp=explode("-", $CurTime);
$year=$temp[0];
$month=$temp[1];
$day=$temp[2];
$hour=$temp[3];
$min=$temp[4];
$sec=$temp[5];
//printf("CurrentTime : %s-%s-%s-%s-%s-%s\n", $year, $month, $day, $hour, $min, $sec);
if (substr($dir,-1) != "/")
{
// 현재 디렉토문자열 뒤에 / 붙이기
$dir .= "/";
}
if (!is_dir($dir))
{
return false;
}
if (($dh = opendir($dir)) !== false)
{
while (($entry = readdir($dh)) !== false)
{
print("$dir$entry\n");
if ($entry != "." && $entry != "..")
{
if (is_file($dir.$entry) || is_link($dir.$entry))
{
// 파일인경우 최근 수정 시간을 알아본다.
$CurFileTime = date("Y-m-d-H-i-s", filemtime($dir.$entry));
// 년 월 일 시 분 초 추출
$temp=explode("-", $CurFileTime);
$fyear=$temp[0];
$fmonth=$temp[1];
$fday=$temp[2];
$fhour=$temp[3];
$fmin=$temp[4];
$fsec=$temp[5];
printf("CurrentFileTime : %d-%d-%d-%d-%d-%d\n", $fyear, $fmonth, $fday, $fhour, $fmin, $fsec);
// 10분 이전의 파일은 삭제한다
printf("CurrentMin-FileMin = %d\n", abs($min-$fmin));
if (abs($min-$fmin) >= 10)
{
print("$dir$entry --> unlink(delete)\n");
unlink($dir.$entry);
}
}
else if (is_dir($dir.$entry))
{
DelDir($dir.$entry);
}
}
}
closedir($dh);
// 파일이 없는 빈 디렉토리일 경우만 삭제된다.
rmdir($dir);
return true;
}
return false;
}
// 디렉토리(폴더) 리스트 알아오기
function DirList($dirPath)
{
if (!is_dir($dirPath))
{
print("$dirPath 없음");
return false;
}
if ($handle = opendir($dirPath))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
if (is_dir($dirPath."/".$file))
{
$dirList[] = $file;
}
}
}
closedir($handle);
}
if (count($dirList)<0)
{
return false;
}
return $dirList;
}
// 날짜 디렉토리(폴더) 찾아서 삭제하기
function CheckTargetDir($BaseDir)
{
/// 현재 시간 알기
$CurTime = date("Y-m-d-H-i-s", time());
// 년 월 일 시 분 초 추출
$temp=explode("-", $CurTime);
$year=$temp[0];
$month=$temp[1];
$day=$temp[2];
$hour=$temp[3];
$min=$temp[4];
$sec=$temp[5];
printf("CurrentTime : %s-%s-%s-%s-%s-%s\n", $year, $month, $day, $hour, $min, $sec);
// 년 폴더 리스트 파악
$YearDirArr = DirList($BaseDir);
// 년 폴더 안에 달 폴더가 있으면
if (count($YearDirArr) > 0)
{
foreach($YearDirArr as $YearDirName)
{
// 현재 년 이전의 폴더가 있다면
if ($year >= $YearDirName)
{
// 년 폴더 안으로 들어가기
$TargetDir = $BaseDir."/".$YearDirName;
printf("Current TargetDir : %s\n", $TargetDir);
// 년 폴더 안의 달 폴더 리스트 파악
$MonthDirArr = DirList($TargetDir);
// 년 폴더 안에 달 폴더가 있으면
if (count($MonthDirArr) > 0)
{
foreach($MonthDirArr as $MonthDirName)
{
// 현재 달 이전의 달 폴더가 있다면
if ($month >= $MonthDirName)
{
// 달 폴더 안으로 들어가기
$TargetDir = $BaseDir."/".$YearDirName."/".$MonthDirName;
printf("Current TargetDir : %s\n", $TargetDir);
// 달 폴더의 안의 일 폴더 리스트 파악
$DayDirArr = DirList($TargetDir);
// 달 폴더 안에 일 폴더가 있으면
if (count($DayDirArr) > 0)
{
foreach($DayDirArr as $DayDirName)
{
printf("Current TargetDir : %s\n", $TargetDir);
// 현재 일보다 2 일전의 폴더가 있다면
//if ($day-2 >= $DayDirName)
{
// 일 폴더를 지워야 할 폴더로 설정
$TargetDir = $BaseDir."/".$YearDirName."/".$MonthDirName."/".$DayDirName;
// 일폴더 지우기
DelDir($TargetDir);
}
}
}
// 달 폴더 안에 일 폴더가 아무것도 없으면
else
{
// 현재 달 폴더 지우기
DelDir($TargetDir);
}
}
}
}
// 년 폴더 안에 달 폴더가 아무것도 없으면
else
{
// 현재 년 폴더 지우기
DelDir($TargetDir);
}
}
}
}
}
$BaseDir = "/home/test";
CheckTargetDir($BaseDir);
?>
Linux PHP 설치 및 설정 방법
# 만약 libxml 일 설치 되어 있지 않다면 설치하기
yum install libxml2-devel
# php 를 다운받고 압축풀기
wget http://kr.php.net/get/php-5.3.6.tar.bz2/from/kr2.php.net/mirror
tar jxvf php-5.3.6.tar.bz2
# configure 스크립트를 다양한 옵션을 주어 실행(자세한 내용은 ./configure --help)
# --prefix 로 설치 디렉토리 명시
# --with-config-file-path 로 apache httpd server conf 경로 명시
# --with-apx2 로 apache httpd apxs 경로 명시
# --with-mysql 로 mysql 경로 명시
./configure --prefix=/home/ysoftman/php --with-config-file-path=/home/ysoftman/httpd/conf --with-apxs2=/home/ysoftman/httpd/bin/apxs --with-mysql=/home/ysoftman/mysql
# 컴파일하고 설치하기
make && make install
# 아파치 웹서버 httpd.conf 에 아래 항목이 없으면 추가
LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php .php3 .php4 .htm .html
# 압축이 풀린 php 소스 디렉토리에서 php.production 을 아파치 설정 파일로 php.ini 파일로 복사
cp -v php-5.3.6/php.ini-production /home/ysoftman/httpd/conf/php.ini
# php.ini 에서 업로드 파일 크기 및 post 사이크 크기 설정
upload_max_filesize = 2M
post_max_size = 8M
# test.html 에 phpinfo를 호출하여 정상작동하는지 확인
<? phpinfo(); ?>
yum install libxml2-devel
# php 를 다운받고 압축풀기
wget http://kr.php.net/get/php-5.3.6.tar.bz2/from/kr2.php.net/mirror
tar jxvf php-5.3.6.tar.bz2
# configure 스크립트를 다양한 옵션을 주어 실행(자세한 내용은 ./configure --help)
# --prefix 로 설치 디렉토리 명시
# --with-config-file-path 로 apache httpd server conf 경로 명시
# --with-apx2 로 apache httpd apxs 경로 명시
# --with-mysql 로 mysql 경로 명시
./configure --prefix=/home/ysoftman/php --with-config-file-path=/home/ysoftman/httpd/conf --with-apxs2=/home/ysoftman/httpd/bin/apxs --with-mysql=/home/ysoftman/mysql
# 컴파일하고 설치하기
make && make install
# 아파치 웹서버 httpd.conf 에 아래 항목이 없으면 추가
LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php .php3 .php4 .htm .html
# 압축이 풀린 php 소스 디렉토리에서 php.production 을 아파치 설정 파일로 php.ini 파일로 복사
cp -v php-5.3.6/php.ini-production /home/ysoftman/httpd/conf/php.ini
# php.ini 에서 업로드 파일 크기 및 post 사이크 크기 설정
upload_max_filesize = 2M
post_max_size = 8M
# test.html 에 phpinfo를 호출하여 정상작동하는지 확인
<? phpinfo(); ?>