가장 빈번하게 사용하는 업로드 부분을 클래스로 구현해 봤습니다.
보시고 부족한 부분은 수정해 주시면 좋겠네요 ^^
<?
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>
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 1930 | 17년 전 | 3003 | ||
| 1929 | 17년 전 | 1879 | ||
| 1928 | 17년 전 | 3992 | ||
| 1927 | 17년 전 | 1409 | ||
| 1926 |
지엔소프트
|
17년 전 | 1126 | |
| 1925 |
보드타는찌니
|
17년 전 | 1243 | |
| 1924 |
choijinhee
|
17년 전 | 1126 | |
| 1923 | 17년 전 | 1388 | ||
| 1922 | 17년 전 | 1746 | ||
| 1921 | 17년 전 | 2337 | ||
| 1920 | 17년 전 | 2415 | ||
| 1919 | 17년 전 | 1419 | ||
| 1918 | 17년 전 | 1168 | ||
| 1917 | 17년 전 | 2437 | ||
| 1916 | 17년 전 | 2869 | ||
| 1915 |
|
17년 전 | 3234 | |
| 1914 | 17년 전 | 3172 | ||
| 1913 | 17년 전 | 2338 | ||
| 1912 | 17년 전 | 3754 | ||
| 1911 | 17년 전 | 3184 | ||
| 1910 | 17년 전 | 5506 | ||
| 1909 | 17년 전 | 2657 | ||
| 1908 | 17년 전 | 1860 | ||
| 1907 | 17년 전 | 1934 | ||
| 1906 | 17년 전 | 1776 | ||
| 1905 | 17년 전 | 1706 | ||
| 1904 | 17년 전 | 2769 | ||
| 1903 | 17년 전 | 1895 | ||
| 1902 | 17년 전 | 1581 | ||
| 1901 | 17년 전 | 2072 | ||
| 1900 |
stuartkim
|
17년 전 | 1137 | |
| 1899 | 17년 전 | 1667 | ||
| 1898 | 17년 전 | 2882 | ||
| 1897 | 17년 전 | 2126 | ||
| 1896 | 17년 전 | 1820 | ||
| 1895 | 17년 전 | 2036 | ||
| 1894 | 17년 전 | 1324 | ||
| 1893 | 17년 전 | 1716 | ||
| 1892 | 17년 전 | 1215 | ||
| 1891 | 17년 전 | 1776 | ||
| 1890 | 17년 전 | 1806 | ||
| 1889 | 17년 전 | 1041 | ||
| 1888 | 17년 전 | 3136 | ||
| 1887 | 17년 전 | 4316 | ||
| 1886 | 17년 전 | 2464 | ||
| 1885 | 17년 전 | 7475 | ||
| 1884 | 17년 전 | 3840 | ||
| 1883 | 17년 전 | 4171 | ||
| 1882 | 17년 전 | 2429 | ||
| 1881 | 17년 전 | 2888 | ||
| 1880 | 17년 전 | 3638 | ||
| 1879 | 17년 전 | 4006 | ||
| 1878 | 17년 전 | 3167 | ||
| 1877 | 17년 전 | 3149 | ||
| 1876 | 17년 전 | 3297 | ||
| 1875 | 17년 전 | 2463 | ||
| 1874 | 17년 전 | 2059 | ||
| 1873 | 17년 전 | 3110 | ||
| 1872 | 17년 전 | 3258 | ||
| 1871 | 17년 전 | 4611 | ||
| 1870 | 17년 전 | 1592 | ||
| 1869 | 17년 전 | 2617 | ||
| 1868 | 17년 전 | 2689 | ||
| 1867 | 17년 전 | 1901 | ||
| 1866 | 17년 전 | 2220 | ||
| 1865 | 17년 전 | 1837 | ||
| 1864 | 17년 전 | 2372 | ||
| 1863 | 17년 전 | 3777 | ||
| 1862 | 17년 전 | 3895 | ||
| 1861 | 17년 전 | 2083 | ||
| 1860 | 17년 전 | 1360 | ||
| 1859 | 17년 전 | 1497 | ||
| 1858 |
|
17년 전 | 1651 | |
| 1857 | 17년 전 | 1634 | ||
| 1856 |
보드타는찌니
|
17년 전 | 1397 | |
| 1855 | 17년 전 | 1543 | ||
| 1854 |
|
17년 전 | 1360 | |
| 1853 | 17년 전 | 1850 | ||
| 1852 | 17년 전 | 2749 | ||
| 1851 | 17년 전 | 1849 | ||
| 1850 |
|
17년 전 | 1403 | |
| 1849 |
|
17년 전 | 1690 | |
| 1848 |
|
17년 전 | 2329 | |
| 1847 | 17년 전 | 1945 | ||
| 1846 |
은사시나무
|
17년 전 | 1264 | |
| 1845 |
갈색야생마
|
17년 전 | 5905 | |
| 1844 |
갈색야생마
|
17년 전 | 3284 | |
| 1843 |
갈색야생마
|
17년 전 | 3178 | |
| 1842 |
갈색야생마
|
17년 전 | 2589 | |
| 1841 |
갈색야생마
|
17년 전 | 2771 | |
| 1840 |
갈색야생마
|
17년 전 | 2719 | |
| 1839 |
갈색야생마
|
17년 전 | 3226 | |
| 1838 |
갈색야생마
|
17년 전 | 2353 | |
| 1837 |
갈색야생마
|
17년 전 | 2471 | |
| 1836 |
갈색야생마
|
17년 전 | 2532 | |
| 1835 |
갈색야생마
|
17년 전 | 2526 | |
| 1834 |
갈색야생마
|
17년 전 | 2668 | |
| 1833 |
갈색야생마
|
17년 전 | 3188 | |
| 1832 |
갈색야생마
|
17년 전 | 2702 | |
| 1831 |
갈색야생마
|
17년 전 | 1427 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기