파일 업로드 기능 구현(2) upload_ok.php

upload.html에서 파일을 선택한 다음 [전송] 버튼을 눌렀을 때 서버로 파일을 전송하는 기능을 하는 프로그램입니다.
성공적으로 파일을 전송하면 다음과 같이 전송된 파일의 각종 정보가 보이도록 해보겠습니다.

자세한 설명은 다음 시간에 합니다~
우선 코드부터 주~~~~욱 훑어보세요…

  1. <?
  2. // 업로드한 파일이 저장될 디렉토리 정의
  3. $target_dir = “up”;  // 서버에 up 이라는 디렉토리가 있어야 한다.
  4. if(strcmp($upfile,”none”)) {   // 파일이 업로드되었을 경우
  5. // 업로드 금지 파일 식별 부분
  6.     $filename = explode(“.”, $upfile_name);
  7.     $extension = $filename[sizeof($filename)-1];
  8.         
  9.     if(!strcmp($extension,”html”) ||
  10.        !strcmp($extension,”htm”) ||
  11.        !strcmp($extension,”php”) ||      
  12.        !strcmp($extension,”inc”))
  13.     {
  14.        echo(“업로드 금지 파일입니다.”);
  15.        exit;
  16.     }
  17. // 동일한 파일이 있는지 확인하는 부분
  18.     $target = $target_dir . “/” . $upfile_name;
  19.     if(file_exists($target)) {
  20.        echo(“동일 파일명 존재”);
  21.        exit;
  22.     }
  23. // 지정된 디렉토리에 파일 저장하는 부분
  24.     if(!copy($upfile,$target)) {   // false일 경우
  25.        echo(“파일 저장 실패”);
  26.        exit;
  27.     }
  28. // 임시 파일을 삭제하는 부분
  29.     if(!unlink($upfile)) { // false일 경우
  30.        echo(“임시 파일 삭제 실패”);
  31.        exit;
  32.     }
  33. ?>  
  34. <html>
  35. <body>
  36.     <table width=”500″ border=”0″ cellspacing=”1″ cellpadding=”2″ align=”center”>
  37.     <tr>
  38.        <td colspan=”2″ align=”center” bgColor=”#CCCCCC” width=”100%”><font size=2><b>>>
  39.             업로드 성공 <<</b></font></td>  
  40.     </tr>
  41.     <tr>
  42.        <td align=”left” bgColor=”#EEEEEE” width=”120″><font size=2>파일명</font></td>
  43.        <td bgColor=”#EEEEEE”><font size=2><?echo(“$upfile_name”)?></font></td>
  44.     </tr>
  45.     <tr>
  46.        <td align=”left” bgColor=”#EEEEEE” width=”120″><font size=2>임시 저장 파일명</font></td>
  47.        <td bgColor=”#EEEEEE”><font size=2><?echo(“$upfile”)?></font></td>
  48.     </tr>
  49.     <tr>
  50.        <td align=”left” bgColor=”#EEEEEE” width=”120″><font size=2>파일 크기(Bytes)</font></td>
  51.        <td bgColor=”#EEEEEE”><font size=2><?echo(“$upfile_size”)?> Bytes</font></td>
  52.     </tr>
  53.     <tr>
  54.        <td align=”left” bgColor=”#EEEEEE” width=”120″><font size=2>파일의 MIME Type</font></td>
  55.        <td bgColor=”#EEEEEE”><font size=2><?echo(“$upfile_type”)?></font></td>
  56.     </tr>
  57.     </table>
  58. <?
  59. } else {
  60. ?>
  61.         <table width=”500″ border=”0″ cellspacing=”1″ cellpadding=”2″ align=”center” bgcolor=”#FFCCFF”>
  62.     <tr>
  63.        <td width=”100%” align=”center” bgColor=”#CCCCCC”><font size=2><b>업로드 실패!!!</b></font></td>
  64.     </tr>
  65.     </table>
  66. </body>
  67. </html>
  68. <?
  69. }
  70. ?>

    이상 동주아빠, 손병목이었습니다.

위로 스크롤