여러 개의 파일을 선택한 다음 실제 서버에 파일을 기록하는 부분입니다.
좀 복잡합니다.
일단 소스부터 보고, 설명은 다음 시간에 하겠습니다.
코드 중간에 최대한 주석을 달아놓았으니 어느 정도 감~은 잡을 수 있을 겁니다.

아래 코드의 실행 결과 화면입니다.(파일을 세 개 첨부했을 경우입니다.)

- <html>
- <head>
- <title>복수 개의 파일 업로드 결과 확인</title>
- <meta http-equiv=”Content-Type” content=”text/html; charset=euc-kr”>
- <style type=”text/css”>
- BODY, TD, INPUT {font-family:굴림;font-size:9pt;color:#000000;}
- </style>
- </head>
- <body>
- <?
- $target_dir = “up”; // 업로드한 파일이 저장될 디렉토리. 서버에 up 이라는 디렉토리가 있어야 한다.
- $uploaded_yn = “N”; // 기본값으로 N 설정. 파일이 하나라도 업로드될 경우 나중에 “Y”로 바꾸게 됨.
- for ($num = 1 ; $num <= 3 ; $num++) { // 업로드한 파일이 최대 3개일 경우
- $upfile = ${“inputname”.$num} ; // ${selectfile1} → $selectfile1 과 동일. 임시 파일명
- $upfile_name = ${“inputname”.$num.”_name”} ; // INPUT 태그의 이름 뒤에 “_name”을 붙이면 실제 파일 이름의 변수가 된다. PHP에서 자동 생성되는 변수명.
- $upfile_size = ${“inputname”.$num.”_size”} ; // 파일 사이즈
- $upfile_type = ${“inputname”.$num.”_type”} ; // 파일 타입
- if(!strcmp($upfile,”none”)) { // $upfile이 “none”이면 0, 아니면 1이 됨. 따라서 업로드되지 않았을 경우에 strcmp 결과는 0이 되고, !strcmp 이므로 if 조건문은 1이 되어 참이 됨. 으~ 복잡해라. 한마디로 파일이 업로드되지 않았을 경우 아래 코드를 실행하라는 뜻.
- continue; // 아래 코드를 실행하지 말고 for 문으로 다시 돌아가라는 뜻.
- } else { // 파일이 업로드되었다면…
- $target = $target_dir . “/” . $upfile_name;
- if(!copy($upfile,$target)) { // false일 경우
- continue;
- }
- $uploaded_yn = “Y”;
- ?>
- <table width=”500″ border=”0″ cellspacing=”1″ cellpadding=”2″ align=”center”>
- <tr>
- <td colspan=”2″ align=”center” bgColor=”#CCCCCC” width=”100%”><font size=2><b>>> 업로드 성공 <<</b></font></td>
- </tr>
- <tr>
- <td align=”left” bgColor=”#EEEEEE” width=”120″><font size=2>파일명</font></td>
- <td bgColor=”#EEEEEE”><font size=2><?echo(“$upfile_name”)?></font></td>
- </tr>
- <tr>
- <td align=”left” bgColor=”#EEEEEE” width=”120″><font size=2>임시 저장 파일명</font></td>
- <td bgColor=”#EEEEEE”><font size=2><?echo(“$upfile”)?></font></td>
- </tr>
- <tr>
- <td align=”left” bgColor=”#EEEEEE” width=”120″><font size=2>파일 크기(Bytes)</font></td>
- <td bgColor=”#EEEEEE”><font size=2><?echo(“$upfile_size”)?> Bytes</font></td>
- </tr>
- <tr>
- <td align=”left” bgColor=”#EEEEEE” width=”120″><font size=2>파일의 MIME Type</font></td>
- <td bgColor=”#EEEEEE”><font size=2><?echo(“$upfile_type”)?></font></td>
- </tr>
- </table>
- <?
- } // 22행의 else 닫기
- if ($uploaded_yn == “N”) {
- ?>
- <table width=”500″ border=”0″ cellspacing=”1″ cellpadding=”2″ align=”center” bgcolor=”#FFCCFF”>
- <tr>
- <td width=”100%” align=”center” bgColor=”#CCCCCC”><font size=2><b>업로드 실패!!!</b></font></td>
- </tr>
- </table>
- </body>
- </html>
- <?
- } // 55행의 if 문 닫기
- } // 15행의 for 문 닫기
- ?>
이상, 동주아빠 손병목이었습니다.
