파일업로드 클래스~~

· 19년 전 · 4225
가장 빈번하게 사용하는 업로드 부분을 클래스로 구현해 봤습니다.

보시고 부족한 부분은 수정해 주시면 좋겠네요 ^^

<?
class FileUpload
{
var $directory;
var $fileInfo;
var $code;
var $limitSize;
var $extension;

/* 클래스 생성자
@ $directory : 파일업로드 디렉토리 경로,
@ $http_file_name : $_FILES[file_name] 정보,
@ $code : 파일명을 코드값을 기준으로 자동부여
@ $limit_size : MB
@ $extension : array 업로드 불가능한 확장자

@ return void */

function FileUpload($directory, $http_file_name, $code='', $limit_size=100, $extension=array('php','cgi','php3','pl','html','htm','exe','jsp')){
$this->directory = $directory;
$this->fileInfo = $http_file_name;
$this->code = $code;
$this->limit_size = $limit_size;
$this->extension = $extension;
}

function delFile($file_name){
if(file_exists($this->directory.'/'.$file_name)){
unlink($this->directory.'/'.$file_name);
return true;
}
else
return false;
}

/* 확장자를 제거한 파일이름을 리턴하는 메소드 */
function getFileName(){
$_file_name = explode('.',$this->fileInfo['name']);
return $_file_name[0];
}

/* 파일의 확장자 추출 메소드 */
function getFileExtension($file_name=''){
if($file_name=='') $file_name = $this->fileInfo['name'];
$_file_name = explode('.',strtolower($file_name));
return $_file_name[count($_file_name)-1];
}

/* 업로드 가능한 확장자 인지 검사 하는 메소드 */
function isValidExtension(){
$ext = $this->getFileExtension();
$isExt = in_array($ext, $this->extension);

if($isExt){
$this->errorMsg("업로드할 수 없는 확장자입니다.");
return false;
}
else
return true;
}

/* 파일 사이즈가 업로드 가능한지 확인 하는 메소드 */
function isLimitFileSize(){
if($this->limit_size * 1024 * 1024 < $fileInfo['size']){
$this->errorMsg("업로드 할 파일의 크기가 초과 하였습니다.");
return false;

}
else
return true;
}

/* 중복된 파일이름이 있을경우 새로운 이름을 부여하는 메소드 */
/* $auto_file_name : 자동파일명 부여 true/false */
function getValidFileName(){

$tmp_file_ext = $this->getFileExtension();

if($this->code!=''){
$file_name = $this->code.date('ymdHis').'.'.$tmp_file_ext;
$tmp_file_name = $this->code.date('ymdHis');
}
else {
$file_name = $this->fileInfo['name'];
$tmp_file_name = $this->getFileName();
}

if(!is_dir($this->directory)){
if(mkdir($this->directory)) chmod($this->directory,0777);
}

$i=1;
while(file_exists($this->directory.'/'.$file_name)){
$file_name = $tmp_file_name.'-'.$i.'.'.$tmp_file_ext;
$i++;
}

return $file_name;
}

function makeDir($dir=''){
if($dir=='') $dir = $this->directory;

$_dir = explode('/', $this->directory);
for($i=0; $i<count($_dir); $i++){
$now_dir .= '/'.$_dir[$i];
if(!file_exists($now_dir)){
mkdir($now_dir,0777);
chmod($now_dir,0777);
}
}
}

/* 파일을 업로드 하는 메소드 */
function uploadFile(){
if($this->isValidExtension() && $this->isLimitFileSize()){

//디렉토리가 없을 경우 자동 생성
// /file에 쓰기권한이 있어야 함
if(!file_exists($this->directory)) $this->makeDir();

$new_file_name = $this->getValidFileName();

if(move_uploaded_file($this->fileInfo['tmp_name'],$this->directory.'/'.$new_file_name)){
$this->fileInfo['rename'] = $new_file_name;
return true;
}
else {
$this->errorMsg("업로드를 실패했습니다. 다시 확인해 주세요");
return false;
}
}
}

function errorMsg($msg){
echo ('<script language="javascript">
alert("'.$msg.'");
</script>');
}
}
?>

사용법은 아래처럼 하시면 됩니다.

<?
include "class.FileUpload.php";

if($_FILES[doc][name]){
$temp_doc = $_FILES[doc][name];

//이미지의 경우 자동번호 부여
$ext = FileUpload::getFileExtension($_FILES[doc][name]);
if(in_array($ext, array('jpg','jpeg','gif','bmp')))
$fileUpload = new FileUpload($UPLOAD_DIR,$_FILES[doc],'IMG');
else
$fileUpload = new FileUpload($UPLOAD_DIR,$_FILES[doc]);

if($fileUpload->uploadFile()) $doc = $fileUpload->fileInfo[rename];
else {
Msg::backMsg("파일을 다시 선택해 주십시오");
exit();
}

echo "<br>".$doc //업로드된 파일명입니다.
}

?><div class='small'>[이 게시물은 관리자님에 의해 2011-10-31 17:12:10 PHP & HTML에서 이동 됨]</div>
|
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
19년 전 조회 3,381
19년 전 조회 5,193
19년 전 조회 3,019
19년 전 조회 3,628
19년 전 조회 3,464
19년 전 조회 2,323
19년 전 조회 3,437
19년 전 조회 2,190
19년 전 조회 2,637
19년 전 조회 2,740
19년 전 조회 4,311
19년 전 조회 3,503
19년 전 조회 3,393
19년 전 조회 2,847
19년 전 조회 2,303
19년 전 조회 1,940
19년 전 조회 2,723
19년 전 조회 2,630
19년 전 조회 2,112
19년 전 조회 2,117
19년 전 조회 2,920
19년 전 조회 2,868
19년 전 조회 2,179
19년 전 조회 2,418
19년 전 조회 3,608
19년 전 조회 2,262
19년 전 조회 2,786
19년 전 조회 2,867
19년 전 조회 2,986
19년 전 조회 3,241
19년 전 조회 2,873
19년 전 조회 3,311
19년 전 조회 3,065
19년 전 조회 3,809
19년 전 조회 2,184
19년 전 조회 3,037
19년 전 조회 2,676
19년 전 조회 2,321
19년 전 조회 2,724
19년 전 조회 2,187
19년 전 조회 5,747
19년 전 조회 2,574
19년 전 조회 4,548
19년 전 조회 2,640
19년 전 조회 2,173
19년 전 조회 2,895
19년 전 조회 2,647
19년 전 조회 2,828
19년 전 조회 2,468
19년 전 조회 4,226
19년 전 조회 3,698
19년 전 조회 2,500
19년 전 조회 2,429
19년 전 조회 2,276
19년 전 조회 2,297
19년 전 조회 4,668
19년 전 조회 3,646
19년 전 조회 3,070
19년 전 조회 3,655
19년 전 조회 3,274
19년 전 조회 3,138
19년 전 조회 2,643
19년 전 조회 2,460
19년 전 조회 2,783
19년 전 조회 3,378
19년 전 조회 2,398
19년 전 조회 3,424
19년 전 조회 3,527
19년 전 조회 3,210
19년 전 조회 2,223
19년 전 조회 1,993
19년 전 조회 2,618
19년 전 조회 2,553
19년 전 조회 1,909
19년 전 조회 1,948
19년 전 조회 1,843
19년 전 조회 2,347
19년 전 조회 1,905
19년 전 조회 2,470
19년 전 조회 2,213
19년 전 조회 2,100
19년 전 조회 1,945
19년 전 조회 2,120
19년 전 조회 2,165
19년 전 조회 2,221
19년 전 조회 2,955
19년 전 조회 5,596
19년 전 조회 2,011
19년 전 조회 1,850
19년 전 조회 1,713
19년 전 조회 1,886
19년 전 조회 1,801
19년 전 조회 1,729
19년 전 조회 1,759
19년 전 조회 1,664
19년 전 조회 1,713
19년 전 조회 3,081
19년 전 조회 4,113
19년 전 조회 5,497
19년 전 조회 1,669