git-svn-id: http://xe-core.googlecode.com/svn/trunk@15 201d5d3c-b55e-5fd7-737f-ddc643e51545

This commit is contained in:
zero 2007-02-14 05:05:30 +00:00
parent cc287e7787
commit c14b78030e

View file

@ -1,94 +1,104 @@
<?php
/**
* @file : classes/file/FileHandler.class.php
* @author : zero <zero@nzeo.com>
* @desc : 파일 시스템 관련 라이브러리
**/
/**
* @class FileHandler
* @author zero (zero@nzeo.com)
* @brief 파일시스템을 쉽게 사용하기 위한 method를 모은 클래스
*
* 굳이 class로 만들필요는 없는데.. 소스 코드의 관리를 위하여..
**/
class FileHandler {
class FileHandler {
// public String readFile()/*{{{*/
function readFile($file_name) {
if(!file_exists($file_name)) return;
if(filesize($file_name)<1) return;
$fp = fopen($file_name, "r");
$buff = fread($fp, filesize($file_name));
fclose($fp);
return trim($buff);
}/*}}}*/
// public String writeFile($file_name, $buff, $mode = "w")/*{{{*/
function writeFile($file_name, $buff, $mode = "w") {
$mode = strtolower($mode);
if($mode != "a") $mode = "w";
if(@!$fp = fopen($file_name,$mode)) return;
fwrite($fp, $buff);
fclose($fp);
}/*}}}*/
// public array readDir($path, $filter = '', $to_lower = flase)/*{{{*/
// $path내의 파일들을 return ('.', '..', '.로 시작하는' 파일들은 제외)
function readDir($path, $filter = '', $to_lower = false, $concat_prefix = false) {
if(substr($path,-1)!='/') $path .= '/';
if(!is_dir($path)) return array();
$oDir = dir($path);
while($file = $oDir->read()) {
if(substr($file,0,1)=='.') continue;
if($filter && !preg_match($filter, $file)) continue;
if($to_lower) $file = strtolower($file);
if($filter) $file = preg_replace($filter, '$1', $file);
else $file = $file;
if($concat_prefix) $file = $path.$file;
$output[] = $file;
}
if(!$output) return array();
return $output;
}/*}}}*/
// public boolean makeDir($path) {/*{{{*/
// 디렉토리 생성
function makeDir($path_string) {
$path_list = explode('/', $path_string);
for($i=0;$i<count($path_list);$i++) {
$path .= $path_list[$i].'/';
if(!is_dir($path)) {
@mkdir($path, 0707);
@chmod($path, 0707);
/**
* @brief 파일의 내용을 읽어서 return
**/
function readFile($file_name) {
if(!file_exists($file_name)) return;
if(filesize($file_name)<1) return;
$fp = fopen($file_name, "r");
$buff = fread($fp, filesize($file_name));
fclose($fp);
return trim($buff);
}
}
return is_dir($path_string);
}/*}}}*/
// public void removeDir($path) /*{{{*/
// 지정된 디렉토리 이하 모두 파일을 삭제
function removeDir($path) {
if(!is_dir($path)) return;
$directory = dir($path);
while($entry = $directory->read()) {
if ($entry != "." && $entry != "..") {
if (is_dir($path."/".$entry)) {
FileHandler::removeDir($path."/".$entry);
} else {
@unlink($path."/".$entry);
}
/**
* @brief $buff의 내용을 파일에 쓰기
**/
function writeFile($file_name, $buff, $mode = "w") {
$mode = strtolower($mode);
if($mode != "a") $mode = "w";
if(@!$fp = fopen($file_name,$mode)) return;
fwrite($fp, $buff);
fclose($fp);
}
}
$directory->close();
@rmdir($path);
}/*}}}*/
// public string filesize($size) /*{{{*/
// byte단위의 파일크기를 적절하게 변환해서 return
function filesize($size) {
if(!$size) return "0Byte";
if($size<1024) return ($size."Byte");
if($size >1024 && $size< 1024 *1024) return sprintf("%0.1fKB",$size / 1024);
return sprintf("%0.2fMB",$size / (1024*1024));
}/*}}}*/
/**
* @brief $path내의 파일들을 return ('.', '..', '.로 시작하는' 파일들은 제외)
**/
function readDir($path, $filter = '', $to_lower = false, $concat_prefix = false) {
if(substr($path,-1)!='/') $path .= '/';
if(!is_dir($path)) return array();
$oDir = dir($path);
while($file = $oDir->read()) {
if(substr($file,0,1)=='.') continue;
if($filter && !preg_match($filter, $file)) continue;
if($to_lower) $file = strtolower($file);
if($filter) $file = preg_replace($filter, '$1', $file);
else $file = $file;
if($concat_prefix) $file = $path.$file;
$output[] = $file;
}
if(!$output) return array();
return $output;
}
}
/**
* @brief 디렉토리 생성
*
* 주어진 경로를 단계별로 접근하여 recursive하게 디렉토리 생성
**/
function makeDir($path_string) {
$path_list = explode('/', $path_string);
for($i=0;$i<count($path_list);$i++) {
$path .= $path_list[$i].'/';
if(!is_dir($path)) {
@mkdir($path, 0707);
@chmod($path, 0707);
}
}
return is_dir($path_string);
}
/**
* @brief 지정된 디렉토리 이하 모두 파일을 삭제
**/
function removeDir($path) {
if(!is_dir($path)) return;
$directory = dir($path);
while($entry = $directory->read()) {
if ($entry != "." && $entry != "..") {
if (is_dir($path."/".$entry)) {
FileHandler::removeDir($path."/".$entry);
} else {
@unlink($path."/".$entry);
}
}
}
$directory->close();
@rmdir($path);
}
/**
* @brief byte단위의 파일크기를 적절하게 변환해서 return
**/
function filesize($size) {
if(!$size) return "0Byte";
if($size<1024) return ($size."Byte");
if($size >1024 && $size< 1024 *1024) return sprintf("%0.1fKB",$size / 1024);
return sprintf("%0.2fMB",$size / (1024*1024));
}
}
?>