PHP에서 CSV 파일 불러오기, 읽기

2015. 3. 24. 16:43IT/PHP

// 파일첨부 내용 시작

<?

$sub_menu = "200900";

include_once("./_common.php");

 

auth_check($auth[$sub_menu], "r");

 

$g4[title] = "엑셀입력";

include_once("./admin.head.php");

 

?>

 

<script language="javascript" src="<?=$g4[path]?>/js/sideview.js"></script>

<script language="JavaScript">

var list_update_php = "member_list_update.php";

var list_delete_php = "member_list_delete.php";

</script>

<form name=forderprint action="./huwon_xls_update.php" method='post' enctype='multipart/form-data'>

<input type=hidden name=case value="2">

<table width=100%>

<tr><td colspan=2 height=2 bgcolor=#DDDDDD></td></tr>

<tr>

    <td width="150" class="pad_t10">엑셀파일 업로드 <b>(*.csv)</b></td>

    <td class="pad_tl10">

<input type=file name=fr_od_id size=30 maxlength=10 class=ed>&nbsp;<b>확장자를 csv로 저장 후 업로드해주세요.</b>

</td>

</tr>

<tr>

<td colspan=2><input type=submit class=btn1 value='  확  인  '></td>

</tr>

</table>

</form>

 

<?

include_once ("./admin.tail.php");

?>

 

// 파일첨부 내용 끝

 

 

 

 

 

// 파일 읽기 시작

 

 

<?

 

 

setlocale(LC_CTYPE, 'ko_KR.eucKR'); 

$sub_menu = "200800";

include_once("./_common.php");

include_once("./admin.head.php");

 

$csvLoad = fopen($_FILES['fr_od_id']['tmp_name'],"r"); 

$i = 1;

while ( $minfo = fgetcsv( $csvLoad, 1000 ) ) {

print_r2($minfo);

$birth = "";

$hp = "";

$h_date = "";

$h_gbn = "";

$h_result = "";

$h_amt = "";

$h_pay = "";

$reg_date = "";

 

if($i > 1){

$birth = iconv("euc-kr","utf-8",$minfo[1]); 

$hp = iconv("euc-kr","utf-8",$minfo[2]); 

$h_date = iconv("euc-kr","utf-8",$minfo[3]); 

$h_gbn = iconv("euc-kr","utf-8",$minfo[4]); 

$h_result = iconv("euc-kr","utf-8",$minfo[5]); 

$h_amt = iconv("euc-kr","utf-8",$minfo[6]); 

$h_pay = iconv("euc-kr","utf-8",$minfo[7]);

$reg_date = $g4[time_ymdhis];

 

// 생일 자르기

$birth = substr(trim($birth), 0, 6);

$hp = trim(str_replace("-","",$hp));

$h_date = trim(str_replace("-","",$h_date));

$h_date = trim(str_replace(".","",$h_date));

$h_gbn = trim(str_replace(".","",$h_gbn));

$h_result = trim(str_replace(".","",$h_result));

$h_amt = trim(str_replace(",","",$h_amt));

$h_pay = (str_replace("원","",$h_pay));

$h_pay = (str_replace(",","",$h_pay));

$h_pay = (str_replace("\"","",$h_pay));

 

 

$sql = "select count(*) as cnt from g4_huwon where birth='$birth' and h_date = '$h_date' and hp = '$hp' ";

$row2 = sql_fetch($sql);

 

 

if($row2[cnt]>0){

$sql = "select * from g4_huwon where birth='$birth' and h_date = '$h_date' and hp = '$hp' limit 0, 1 ";

$row33 = sql_fetch($sql);

 

$sql=" update g4_huwon set

birth='$birth',

hp='$hp',

h_date='$h_date',

h_gbn='$h_gbn',

h_result='$h_result',

h_amt='$h_amt',

h_pay='$h_pay'

where h_idx='$row33[h_idx]'";

}else{

$sql=" insert into g4_huwon set

birth='$birth',

hp='$hp',

h_date='$h_date',

h_gbn='$h_gbn',

h_result='$h_result',

h_amt='$h_amt',

h_pay='$h_pay'

";

 

}

echo $sql."<BR><BR><BR>";

//sql_query($sql);

}

$i++;

}

 

//$csvArray = split("\n",implode($csvLoad));

 

 

$num=count($csvArray)-2;

die();

echo "{$num}건 변환 완료 되었습니다.";

echo "<br><input type='button' class=btn1 value='완료' onclick=\"location.href('$g4[path]/adm/member_list.php');\">";

include_once ("./admin.tail.php");

 

?> 

 

 

// 파일 읽기 끝