가장 빈번하게 사용하는 업로드 부분을 클래스로 구현해 봤습니다.
보시고 부족한 부분은 수정해 주시면 좋겠네요 ^^
<?
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>
보시고 부족한 부분은 수정해 주시면 좋겠네요 ^^
<?
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>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 1030 | 18년 전 | 1787 | ||
| 1029 | 18년 전 | 2217 | ||
| 1028 | 18년 전 | 2630 | ||
| 1027 | 18년 전 | 1791 | ||
| 1026 | 18년 전 | 1523 | ||
| 1025 | 18년 전 | 1539 | ||
| 1024 | 18년 전 | 1463 | ||
| 1023 | 18년 전 | 3224 | ||
| 1022 | 18년 전 | 2911 | ||
| 1021 | 18년 전 | 1675 | ||
| 1020 | 18년 전 | 3409 | ||
| 1019 | 18년 전 | 1835 | ||
| 1018 | 18년 전 | 1489 | ||
| 1017 | 18년 전 | 1954 | ||
| 1016 | 18년 전 | 2096 | ||
| 1015 | 18년 전 | 1631 | ||
| 1014 | 18년 전 | 2343 | ||
| 1013 | 18년 전 | 1998 | ||
| 1012 | 18년 전 | 1621 | ||
| 1011 | 18년 전 | 1783 | ||
| 1010 | 18년 전 | 2038 | ||
| 1009 | 18년 전 | 1799 | ||
| 1008 | 18년 전 | 1606 | ||
| 1007 | 18년 전 | 1660 | ||
| 1006 | 18년 전 | 3131 | ||
| 1005 | 18년 전 | 1992 | ||
| 1004 | 18년 전 | 1866 | ||
| 1003 | 18년 전 | 4366 | ||
| 1002 | 18년 전 | 4993 | ||
| 1001 | 18년 전 | 3158 | ||
| 1000 | 18년 전 | 2908 | ||
| 999 | 18년 전 | 3497 | ||
| 998 | 18년 전 | 2043 | ||
| 997 | 18년 전 | 2401 | ||
| 996 | 18년 전 | 2695 | ||
| 995 | 18년 전 | 2751 | ||
| 994 | 18년 전 | 2346 | ||
| 993 | 18년 전 | 1612 | ||
| 992 | 18년 전 | 3376 | ||
| 991 | 18년 전 | 1983 | ||
| 990 | 18년 전 | 2379 | ||
| 989 | 18년 전 | 2453 | ||
| 988 | 18년 전 | 2881 | ||
| 987 | 18년 전 | 5213 | ||
| 986 |
|
18년 전 | 2489 | |
| 985 |
|
18년 전 | 3341 | |
| 984 | 18년 전 | 4275 | ||
| 983 |
느낌좋은날
|
18년 전 | 2500 | |
| 982 | 19년 전 | 2464 | ||
| 981 | 19년 전 | 4037 | ||
| 980 | 19년 전 | 2747 | ||
| 979 |
Sinpre
|
19년 전 | 2989 | |
| 978 | 19년 전 | 2206 | ||
| 977 | 19년 전 | 2217 | ||
| 976 | 19년 전 | 1990 | ||
| 975 | 19년 전 | 3273 | ||
| 974 | 19년 전 | 2105 | ||
| 973 |
|
19년 전 | 2782 | |
| 972 |
|
19년 전 | 1742 | |
| 971 | 19년 전 | 5314 | ||
| 970 | 19년 전 | 3570 | ||
| 969 | 19년 전 | 4320 | ||
| 968 | 19년 전 | 3065 | ||
| 967 | 19년 전 | 2523 | ||
| 966 | 19년 전 | 3919 | ||
| 965 | 19년 전 | 2648 | ||
| 964 | 19년 전 | 2991 | ||
| 963 | 19년 전 | 2958 | ||
| 962 | 19년 전 | 4174 | ||
| 961 | 19년 전 | 3126 | ||
| 960 | 19년 전 | 3021 | ||
| 959 | 19년 전 | 3671 | ||
| 958 | 19년 전 | 2974 | ||
| 957 | 19년 전 | 2190 | ||
| 956 | 19년 전 | 2222 | ||
| 955 | 19년 전 | 2174 | ||
| 954 | 19년 전 | 2766 | ||
| 953 | 19년 전 | 2474 | ||
| 952 | 19년 전 | 3344 | ||
| 951 | 19년 전 | 3202 | ||
| 950 | 19년 전 | 1826 | ||
| 949 | 19년 전 | 2740 | ||
| 948 | 19년 전 | 7824 | ||
| 947 |
|
19년 전 | 2501 | |
| 946 |
DeepnBlue
|
19년 전 | 3102 | |
| 945 |
pearly
|
19년 전 | 2867 | |
| 944 | 19년 전 | 3021 | ||
| 943 |
|
19년 전 | 2224 | |
| 942 | 19년 전 | 2452 | ||
| 941 |
|
19년 전 | 4070 | |
| 940 | 19년 전 | 2231 | ||
| 939 |
|
19년 전 | 4219 | |
| 938 |
|
19년 전 | 2382 | |
| 937 | 19년 전 | 2073 | ||
| 936 |
|
19년 전 | 2327 | |
| 935 | 19년 전 | 1872 | ||
| 934 |
|
19년 전 | 2406 | |
| 933 | 19년 전 | 2196 | ||
| 932 |
|
19년 전 | 3065 | |
| 931 | 19년 전 | 1853 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기