Web/PHP
[self] 클래스 업로드
aucd29
2013. 9. 26. 21:37
<?
class Upload
{
var $objCommon;
var $upRoot;
var $upType;
var $upfileName;
var $aPathInfo = array();
var $sOnlyName;
var $thumb;
var $aDenyExt = array();
var $aAllowExt = array();
function Upload( )
{
$this->objCommon = & $GLOBALS[objCommon]; # 오류창 처리
$this->upRoot = & $GLOBALS[uploadRoot]; # 업로드 디렉토리
$this->upType = & $GLOBALS[upType]; # 업로드 타입 ex> img, user, file
$this->upfileName = & $GLOBALS[upfileName]; # 파일 이름 형식 time, name
$this->thumb = & $GLOBALS[thumb]; # 썸네일 만들기
if( $this->upType == 'img' )
{
$this->aAllowExt = array(
'gif','jpg','jpeg','swf'
);
}
else if( $this->upType == 'user' )
{
$aUserType = $GLOBALS[aUserType];
if( $aUserType[allow] )
{
$this->aAllowExt = $aUserType[allow];
}
else
{
$this->aDenyExt = $aUserType[deny];
}
}
else
{
$this->aDenyExt = array(
'php','php3','cgi','html','htm','pl'
);
}
}
function isUploadedFile( $sFile )
{
if( $sFile )
{
$this->aPathInfo = pathinfo( $sFile );
# img와 file의 구분해서 되는걸 체크하는지 안되는걸 체크하는지 보자.
if( $this->upType == 'img' )
{
if (!in_array( strtolower( $this->aPathInfo[extension] ), $this->aAllowExt ) )
{
$sAllowExt = implode( ", ", $this->aAllowExt );
$this->objCommon->msg( $sAllowExt.' 만 업로드가능합니다.');
}
}
else
{
if ( in_array( strtolower( $this->arrPathInfo[extension] ), $this->aDenyExt ) )
{
$sDenyExt = implode( ", ", $this->aDenyExt );
$this->objCommon->msg( '확장자 '.$sDenyExt.' 는 업로드가 불가능합니다.');
}
}
}
}
# 파일명 중복 체크
function getUniqFileName( )
{
# 파일명이 time이면 타임 형식으로다가 이름을 리턴해준다.
if( $this->upfileName == 'time' )
{
$sFileName = time().'.'.$this->aPathInfo[extension];
}
else
{
$this->sOnlyName = basename( $this->aPathInfo[basename], ".".$this->aPathInfo[extension] );
$sFile = $this->upRoot.'/'.$this->aPathInfo[basename];
$sFileName = $this->aPathInfo[basename];
# 동일 파일명 검사
$i = 0;
do
{
if ( $i )
{
$sFile = sprintf("%s/%s(%d).%s", $this->upRoot, $this->sOnlyName, $i, $this->aPathInfo[extension] );
$sFileName = sprintf("%s(%d).%s", $this->sOnlyName, $i, $this->aPathInfo[extension] );
}
$i++;
}
while( file_exists( $sFile ) );
}
return $sFileName;
}
# 업로드 디렉토리 검사
function uploadDirChk( )
{
if( !$this->upRoot )
{
$this->objCommon->msg( '업로드 디렉토리를 설정해주세요', NULL );
}
else
{
if( !is_dir( $this->upRoot ) )
{
@mkdir( $this->upRoot ,0777 );
}
}
}
function Thumbnail( &$sFileName )
{
$sImgRoot = $this->upRoot.'/'.$sFileName;
$sThumbRoot = $this->upRoot.'/thumb';
if( !is_dir( $sThumbRoot ) )
{
@mkdir( $sThumbRoot ,0777 );
}
$aImg = getimagesize( $sImgRoot );
if( $this->aPathInfo[extension] == 'jpg' )
{
$tmpImage = imagecreatefromjpeg( $sImgRoot );
}
else if( $this->aPathInfo[extension] == 'gif' )
{
$tmpImage = imagecreatefromgif( $sImgRoot );
}
else
{
$tmpImage = imagecreatefrompng( $sImgRoot );
}
$Image = imagecreate( $this->thumb[0], $this->thumb[1] );
imagecopyresized( $Image, $tmpImage, 0, 0, 0, 0, $this->thumb[0], $this->thumb[1], $aImg[0], $aImg[1] );
$Thumbnail = $sThumbRoot.'/'.$sFileName;
if( $this->aPathInfo[extension] == 'jpg' )
{
imagejpeg( $Image, $Thumbnail );
}
else if( $this->aPathInfo[extension] == 'gif' )
{
imagegif( $Image, $Thumbnail );
}
else
{
imagepng( $Image, $Thumbnail );
}
#exif_thumbnail도 있땅.
}
function uploads( $upload, $upOneBon=NULL )
{
$this->uploadDirChk();
if( is_array( $upload[name] ) )
{
# 먼저 올릴수 있는것인지를 검사하고
for( $i=0; $i<count( $upload[name] ); $i++ )
{
$this->isUploadedFile( $upload[name][$i] );
}
for( $i=0; $i<count( $upload[name] ); $i++ )
{
$this->aPathInfo = pathinfo( $upload[name][$i] );
if( $upOneBon ) $upOneBon[$i] = trim($upOneBon[$i]);
if( $upload[name][$i] )
{
$sFileName = $this->getUniqFileName();
if( !@move_uploaded_file( $upload[tmp_name][$i], $this->upRoot.'/'.$sFileName ))
{
$this->objCommon->msg('업로드 실패');
}
if( $this->thumb )
{
$this->Thumbnail( $sFileName );
}
# 업로드랑 이전 파일이름이랑 다를땐 삭제해블자. 이전 파일을
if( $upOneBon[$i] != $sFileName )
{
@unlink( "$this->upRoot/$upOneBon[$i]" );
}
$sFileNames .= $sFileName.'|';
}
else if( $upOneBon[$i] && !$upload[name] )
{
$sFileNames .= $upOneBon[$i].'|';
}
else
{
$sFileNames .= '|';
}
}
$sFileName = $sFileNames;
}
else
{
$this->isUploadedFile( $upload[name] );
if( $upload[name] )
{
$sFileName = $this->getUniqFileName();
if( !@move_uploaded_file( $upload[tmp_name], $this->upRoot.'/'.$sFileName ))
{
$this->objCommon->msg('업로드 실패');
}
if( $this->thumb )
{
$this->Thumbnail( $sFileName );
}
# 업로드랑 이전 파일이름이랑 다를땐 삭제해블자. 이전 파일을
if( $upOneBon != $sFileName )
{
@unlink( "$this->upRoot/$upOneBon" );
}
}
else if( $upOneBon && !$upload[name] )
{
$sFileName = $upOneBon;
}
else
{
$sFileName = '';
}
}
return $sFileName;
}
function oldUploadImg( $upRoot, $upload, $upload_name, $upOneBon )
{
$aPathinfo = pathinfo( $upload_name );
if( $upload )
{
$aExt = array(
'gif','jpg','jpeg','png','swf'
);
if( !in_array( $aPathinfo[extension], $aExt ) )
{
$sExtension = implode(',',$aExt);
echo"
<script> alert($sExtension.'만 업로드 가능합니다..'); history.back(); </script>";
}
$sFilename = time().".$sExt";
if( !move_uploaded_file( $upload, "$upRoot/$sFilename" ))
{
echo"
<script> alert('업로드 실패'); history.back(); </script>";
}
else
{
if( $upOneBon != $sFilename )
{
@unlink( "$upRoot/$upOneBon" );
}
}
}
else if( $upOneBon && !$upload_name )
{
$sFilename = $upOneBon;
}
else
{
$sFilename = '';
}
return $sFilename;
}
function down($szFile)
{
$arrPathInfo = pathinfo($szFile);
header("Content-type: application/octet-stream\r\n");
header("Content-Disposition: attachment; filename=$arrPathInfo[basename]\r\n\r\n");
header("Content-Transfer-Encoding: binary\r\n");
header("Pragma:no-cache");
header("Expires:0");
$fp = @fopen($szFile, "rb");
if (!$fp)
{
fclose($fp);
return FALSE;
}
else
{
fpassthru($fp);
}
}
# 다운의 새로운 함수 대용량 파일 받을때 유리
function down2( $sFile )
{
$aPathinfo = pathinfo( $sFile );
header("Content-type: application/octet-stream\r\n");
header("Content-Disposition: attachment; filename=$arrPathInfo[basename]\r\n\r\n");
header("Content-Transfer-Encoding: binary\r\n");
header("Pragma:no-cache");
header("Expires:0");
@readfile($sFile);
}
}
if( !$objUpload )
{
$objUpload = & new Upload;
}
?>
class Upload
{
var $objCommon;
var $upRoot;
var $upType;
var $upfileName;
var $aPathInfo = array();
var $sOnlyName;
var $thumb;
var $aDenyExt = array();
var $aAllowExt = array();
function Upload( )
{
$this->objCommon = & $GLOBALS[objCommon]; # 오류창 처리
$this->upRoot = & $GLOBALS[uploadRoot]; # 업로드 디렉토리
$this->upType = & $GLOBALS[upType]; # 업로드 타입 ex> img, user, file
$this->upfileName = & $GLOBALS[upfileName]; # 파일 이름 형식 time, name
$this->thumb = & $GLOBALS[thumb]; # 썸네일 만들기
if( $this->upType == 'img' )
{
$this->aAllowExt = array(
'gif','jpg','jpeg','swf'
);
}
else if( $this->upType == 'user' )
{
$aUserType = $GLOBALS[aUserType];
if( $aUserType[allow] )
{
$this->aAllowExt = $aUserType[allow];
}
else
{
$this->aDenyExt = $aUserType[deny];
}
}
else
{
$this->aDenyExt = array(
'php','php3','cgi','html','htm','pl'
);
}
}
function isUploadedFile( $sFile )
{
if( $sFile )
{
$this->aPathInfo = pathinfo( $sFile );
# img와 file의 구분해서 되는걸 체크하는지 안되는걸 체크하는지 보자.
if( $this->upType == 'img' )
{
if (!in_array( strtolower( $this->aPathInfo[extension] ), $this->aAllowExt ) )
{
$sAllowExt = implode( ", ", $this->aAllowExt );
$this->objCommon->msg( $sAllowExt.' 만 업로드가능합니다.');
}
}
else
{
if ( in_array( strtolower( $this->arrPathInfo[extension] ), $this->aDenyExt ) )
{
$sDenyExt = implode( ", ", $this->aDenyExt );
$this->objCommon->msg( '확장자 '.$sDenyExt.' 는 업로드가 불가능합니다.');
}
}
}
}
# 파일명 중복 체크
function getUniqFileName( )
{
# 파일명이 time이면 타임 형식으로다가 이름을 리턴해준다.
if( $this->upfileName == 'time' )
{
$sFileName = time().'.'.$this->aPathInfo[extension];
}
else
{
$this->sOnlyName = basename( $this->aPathInfo[basename], ".".$this->aPathInfo[extension] );
$sFile = $this->upRoot.'/'.$this->aPathInfo[basename];
$sFileName = $this->aPathInfo[basename];
# 동일 파일명 검사
$i = 0;
do
{
if ( $i )
{
$sFile = sprintf("%s/%s(%d).%s", $this->upRoot, $this->sOnlyName, $i, $this->aPathInfo[extension] );
$sFileName = sprintf("%s(%d).%s", $this->sOnlyName, $i, $this->aPathInfo[extension] );
}
$i++;
}
while( file_exists( $sFile ) );
}
return $sFileName;
}
# 업로드 디렉토리 검사
function uploadDirChk( )
{
if( !$this->upRoot )
{
$this->objCommon->msg( '업로드 디렉토리를 설정해주세요', NULL );
}
else
{
if( !is_dir( $this->upRoot ) )
{
@mkdir( $this->upRoot ,0777 );
}
}
}
function Thumbnail( &$sFileName )
{
$sImgRoot = $this->upRoot.'/'.$sFileName;
$sThumbRoot = $this->upRoot.'/thumb';
if( !is_dir( $sThumbRoot ) )
{
@mkdir( $sThumbRoot ,0777 );
}
$aImg = getimagesize( $sImgRoot );
if( $this->aPathInfo[extension] == 'jpg' )
{
$tmpImage = imagecreatefromjpeg( $sImgRoot );
}
else if( $this->aPathInfo[extension] == 'gif' )
{
$tmpImage = imagecreatefromgif( $sImgRoot );
}
else
{
$tmpImage = imagecreatefrompng( $sImgRoot );
}
$Image = imagecreate( $this->thumb[0], $this->thumb[1] );
imagecopyresized( $Image, $tmpImage, 0, 0, 0, 0, $this->thumb[0], $this->thumb[1], $aImg[0], $aImg[1] );
$Thumbnail = $sThumbRoot.'/'.$sFileName;
if( $this->aPathInfo[extension] == 'jpg' )
{
imagejpeg( $Image, $Thumbnail );
}
else if( $this->aPathInfo[extension] == 'gif' )
{
imagegif( $Image, $Thumbnail );
}
else
{
imagepng( $Image, $Thumbnail );
}
#exif_thumbnail도 있땅.
}
function uploads( $upload, $upOneBon=NULL )
{
$this->uploadDirChk();
if( is_array( $upload[name] ) )
{
# 먼저 올릴수 있는것인지를 검사하고
for( $i=0; $i<count( $upload[name] ); $i++ )
{
$this->isUploadedFile( $upload[name][$i] );
}
for( $i=0; $i<count( $upload[name] ); $i++ )
{
$this->aPathInfo = pathinfo( $upload[name][$i] );
if( $upOneBon ) $upOneBon[$i] = trim($upOneBon[$i]);
if( $upload[name][$i] )
{
$sFileName = $this->getUniqFileName();
if( !@move_uploaded_file( $upload[tmp_name][$i], $this->upRoot.'/'.$sFileName ))
{
$this->objCommon->msg('업로드 실패');
}
if( $this->thumb )
{
$this->Thumbnail( $sFileName );
}
# 업로드랑 이전 파일이름이랑 다를땐 삭제해블자. 이전 파일을
if( $upOneBon[$i] != $sFileName )
{
@unlink( "$this->upRoot/$upOneBon[$i]" );
}
$sFileNames .= $sFileName.'|';
}
else if( $upOneBon[$i] && !$upload[name] )
{
$sFileNames .= $upOneBon[$i].'|';
}
else
{
$sFileNames .= '|';
}
}
$sFileName = $sFileNames;
}
else
{
$this->isUploadedFile( $upload[name] );
if( $upload[name] )
{
$sFileName = $this->getUniqFileName();
if( !@move_uploaded_file( $upload[tmp_name], $this->upRoot.'/'.$sFileName ))
{
$this->objCommon->msg('업로드 실패');
}
if( $this->thumb )
{
$this->Thumbnail( $sFileName );
}
# 업로드랑 이전 파일이름이랑 다를땐 삭제해블자. 이전 파일을
if( $upOneBon != $sFileName )
{
@unlink( "$this->upRoot/$upOneBon" );
}
}
else if( $upOneBon && !$upload[name] )
{
$sFileName = $upOneBon;
}
else
{
$sFileName = '';
}
}
return $sFileName;
}
function oldUploadImg( $upRoot, $upload, $upload_name, $upOneBon )
{
$aPathinfo = pathinfo( $upload_name );
if( $upload )
{
$aExt = array(
'gif','jpg','jpeg','png','swf'
);
if( !in_array( $aPathinfo[extension], $aExt ) )
{
$sExtension = implode(',',$aExt);
echo"
<script> alert($sExtension.'만 업로드 가능합니다..'); history.back(); </script>";
}
$sFilename = time().".$sExt";
if( !move_uploaded_file( $upload, "$upRoot/$sFilename" ))
{
echo"
<script> alert('업로드 실패'); history.back(); </script>";
}
else
{
if( $upOneBon != $sFilename )
{
@unlink( "$upRoot/$upOneBon" );
}
}
}
else if( $upOneBon && !$upload_name )
{
$sFilename = $upOneBon;
}
else
{
$sFilename = '';
}
return $sFilename;
}
function down($szFile)
{
$arrPathInfo = pathinfo($szFile);
header("Content-type: application/octet-stream\r\n");
header("Content-Disposition: attachment; filename=$arrPathInfo[basename]\r\n\r\n");
header("Content-Transfer-Encoding: binary\r\n");
header("Pragma:no-cache");
header("Expires:0");
$fp = @fopen($szFile, "rb");
if (!$fp)
{
fclose($fp);
return FALSE;
}
else
{
fpassthru($fp);
}
}
# 다운의 새로운 함수 대용량 파일 받을때 유리
function down2( $sFile )
{
$aPathinfo = pathinfo( $sFile );
header("Content-type: application/octet-stream\r\n");
header("Content-Disposition: attachment; filename=$arrPathInfo[basename]\r\n\r\n");
header("Content-Transfer-Encoding: binary\r\n");
header("Pragma:no-cache");
header("Expires:0");
@readfile($sFile);
}
}
if( !$objUpload )
{
$objUpload = & new Upload;
}
?>