<HTML>
<HEAD>
<TITLE>Baro Upload Test Form </TITLE>
<style type="text/css">
div,td { font-family: tahoma; font-size: 9pt }
input { border:1px solid; border-color:#CCCCCC }
textarea { border:1px solid; border-color:#CCCCCC }
</style>

<script language="vbscript">
	dim aaa
	dim str = "<a href=""/"">home</a>"
function AttachFiles
	document.JoyUpLoad.AttachFiles
end function
</script>

<? ?>
<!--aaa#include virtual="/include/nocache.asp"-->
<!--bbb#include virtual="/include/dbconn.asp"-->

<?
aa = "http://aaa";
set_time_limit(0);
$g_save_dir="../upload/";

extract($_POST);
extract($_SERVER);
extract($_FILES);

$addfilelist = array();  

// 파일명 중복 체크 
function confirmFname($Fname) {  #파일명,폴더명 매개변수 
	global $g_save_dir;	
	$t_MapPath = $g_save_dir; #저장될 디렉토리 
	
	if(strpos($Fname,'.') > 0) { #확장자가 있으면 
		$attach_file = explode(".",$Fname);  #파일명 분리 
		$strExt= array_pop($attach_file);      #확장자
		$strName = implode(".",$attach_file);       #파일명
		$bExist = True ;  #일단 파일이 있다고 가정하는 불린 변수 
		$strName = str_replace(" ", "_", $strName); 
		$strName = str_replace("&nbsp;", "_", $strName);	
		$strFileName = $t_MapPath . $strName . "." . $strExt;  #전체 경로 
		$FileName = $strName . "." . $strExt;
	}
	else {
		$strName = $Fname;       #파일명
		$bExist = True ;  #일단 파일이 있다고 가정하는 불린 변수 
		$strName = str_replace(" ", "_", $strName); 
		$strName = str_replace("&nbsp;", "_", $strName);	
		$strFileName = $t_MapPath . $strName ;  #전체 경로 
		$FileName = $strName;
	}

	$countFileName = 0; 
	If (file_exists($strFileName)) { 
        while ($bExist)  {                  #우선 있다고 생각 
               If (file_exists($strFileName)) { 
                       $countFileName = $countFileName + 1 ; 
					   if(strpos($Fname,'.') > 0) { #확장자가 있으면 
                       		$FileName1 = $strName . "_" . $countFileName . "." . $strExt; 
					   }
					   else {
							$FileName1 = $strName . "_" . $countFileName;
					   }

                       $strFileName  = $t_MapPath . $FileName1; 
               } else { 
                       $bExist = False; 
               } 
        } 
              return $FileName1; 
	}else{ 
		return $FileName; 
	} 
} 


 
function fileUpload($img_tmp,$img_name,$img_size,$img_type){
	global $g_save_dir;

        if(!$img_tmp){ 
        	    echo "업로드할 자료가 없습니다"."<br>";
        	    echo "저장할 경로:".$g_save_dir."<br>";
                exit;
        }
        if ($img_name) {
                $savepath = $g_save_dir.$img_name;
                if (file_exists($savepath)){
				echo "같은이름의 파일이 등록되어 있습니다."."<br>";
				echo "저장할 경로:".$savepath."<br>";
                exit;
                }
                if (is_uploaded_file($img_tmp)) {
                        if(move_uploaded_file($img_tmp,$savepath)){
                        	echo "파일업로드 성공! 저장위치: ";
                        	print_r($savepath);
                        	echo "<br>";	
                         } 
                }else{
				echo "파일등록을 실패했습니다."."<br>";
				echo "저장할 경로:".$savepath."<br>";
                exit;
                }
        }
        
}

if(is_uploaded_file($_FILES['imgfile']['tmp_name'])) {
		$file_name = $_FILES['imgfile']['name'];
		$file_tmp_name = $_FILES['imgfile']['tmp_name'];
		$file_size = $_FILES['imgfile']['size'];
		$file_type = $_FILES['imgfile']['type'];
		$file_type = $_FILES['imgfile']['error'];
		
	if($file_error) { echo "file error $file_error"; exit; }
	$fn1 = confirmFname($file_name);
   	fileUpload($file_tmp_name,$fn1,$file_size,$file_type);  
}

if(is_uploaded_file($_FILES['imgfile2']['tmp_name'])) {
		$file_name = $_FILES['imgfile2']['name'];
		$file_tmp_name = $_FILES['imgfile2']['tmp_name'];
		$file_size = $_FILES['imgfile2']['size'];
		$file_type = $_FILES['imgfile2']['type'];
		$file_type = $_FILES['imgfile2']['error'];
		
	if($file_error) { echo "file error $file_error"; exit; }
	$fn2 = confirmFname($file_name);
   	fileUpload($file_tmp_name,$fn2,$file_size,$file_type);  
}


for($i=0; $i<count($_FILES['file']['name']); $i++) {
  if(is_uploaded_file($_FILES['file']['tmp_name'][$i])) {
		$file_name = $_FILES['file']['name'][$i];
		$file_tmp_name = $_FILES['file']['tmp_name'][$i];
		$file_size = $_FILES['file']['size'][$i];
		$file_type = $_FILES['file']['type'][$i];
		$file_error = $_FILES['file']['error'][$i];
	
	if($file_error) { echo "file error $file_error"; exit; }
	$fn = confirmFname($file_name);

   	fileUpload($file_tmp_name,$fn,$file_size,$file_type); 
	}
}


// response 헤더값 출력 
echo "<b>apache_response_headers</b> <br>";
$headers = apache_response_headers ();
foreach ($headers as $header => $value) {
   echo "$header: $value <br />\n";
}


// form 에서 넘어온 값들 

echo "
<table width='100%' border='1' cellpadding='3' cellspacing='0'>
  <tr>
    <th bgcolor='#FFCC66' scope='col'>필드명</th>
    <th bgcolor='#FFCC66' scope='col'>필드타잎</th>
    <th bgcolor='#FFCC66' scope='col'>필드값</th>
  </tr>
  <tr>
    <td><div align='center'>name</div></td>
    <td><div align='center'>text</div></td>
    <td><div align='center'>$name&nbsp;</div></td>
  </tr>
  <tr>
    <td><div align='center'>subject</div></td>
    <td><div align='center'>text</div></td>
    <td><div align='center'>$subject &nbsp;</div></td>
  </tr>
  <tr>
    <td><div align='center'>pass</div></td>
    <td><div align='center'>password</div></td>
    <td><div align='center'>$pass &nbsp;</div></td>
  </tr>  
  <tr>
    <td><div align='center'>content</div></td>
    <td><div align='center'>text</div></td>
    <td><div align='center'>$content&nbsp;</div></td>
  </tr>
  <tr>
    <td><div align='center'>html_ok</div></td>
    <td><div align='center'>radio</div></td>
    <td><div align='center'>$html_ok&nbsp;</div></td>
  </tr>
  <tr>
    <td><div align='center'>key</div></td>
    <td><div align='center'>hidden</div></td>
    <td><div align='center'>$key&nbsp;</div></td>
  </tr>
  <tr>
    <td><div align='center'>type_list</div></td>
    <td><div align='center'>select</div></td>
    <td><div align='center'>$type_list&nbsp;</div></td>
  </tr>
  <tr>
    <td><div align='center'>color</div></td>
    <td><div align='center'>checkbox</div></td>
    <td><div align='center'>$color&nbsp;</div></td>
  </tr>
   <tr>
    <td><div align='center'>hobby</div></td>
    <td><div align='center'>select multiple</div></td>
    <td><div align='center'>$hobby&nbsp;</div></td>
  </tr>
 
  <tr>
    <td><div align='center'>imgfile</div></td>
    <td><div align='center'>file</div></td>
    <td><div align='center'>$imgfile_name&nbsp;</div></td>
  </tr>
  <tr>
    <td><div align='center'>imgfile2</div></td>
    <td><div align='center'>file</div></td>
    <td><div align='center'>$imgfile2_name&nbsp;</div></td>
  </tr>  
</table>
";

echo "<br><b>폼 데이타 목록 정보</b><pre>";
echo "<pre>";
print_r($_POST);
echo "</pre>";
//전송된 파일 목록 출력 
echo "<br><b>전송된 파일 목록 정보</b><pre>";
echo "<pre>";
print_r($HTTP_POST_FILES); 
echo "</pre>";

// request 헤더값 출력 
echo "<br><br><br> <b>apache_request_headers</b><br>";
$headers = apache_request_headers();
echo "<pre>";
foreach ($headers as $header => $value) {
   echo "$header: $value <br>";
}
echo "</pre>";

?>

<!--aaa#include virtual="/include/nocache.asp"-->
<!--bbbinclude virtual="/include/dbconn.asp"-->
