280 likes | 505 Views
14 장 자료실 제작. 첨부파일의 정보 처리 방법 이해 첨부파일을 서버에 저장 첨파푸일을 화면에 출력 첨부파일 다운로드 기법. 자료실의 개요 DB 테이블 설계 및 생성 첨부파일 업로드 처리 첨부파일 다운로드 처리. 1.1 결과 화면과 요구사항. [ 그림 14-1] 자료실의 글 내용보기 페이지. 1.1 결과 화면과 요구사항. 요구사항 세 개의 첨부파일 업 / 다운로드 가능 다섯 개의 첨부파일을 업로드할 수 있는 DB 설계 ( 추후 확장성고려 )
E N D
첨부파일의 정보 처리 방법 이해 • 첨부파일을 서버에 저장 • 첨파푸일을 화면에 출력 • 첨부파일 다운로드 기법
자료실의 개요 • DB 테이블 설계 및 생성 • 첨부파일 업로드 처리 • 첨부파일 다운로드 처리
1.1 결과 화면과 요구사항 [그림 14-1] 자료실의 글 내용보기 페이지
1.1 결과 화면과 요구사항 • 요구사항 • 세 개의 첨부파일 업/다운로드 가능 • 다섯 개의 첨부파일을 업로드할 수 있는 DB 설계(추후 확장성고려) • 첨부파일의 크기는 500KB 이하, 파일의 확장자 관련없이 업/다운로드 가능 • 업로드된 첨부파일명 변경을 통한 업로드 파일명 중복 방지 • HTML 쓰기 기능 제거
1.2 준비 파일과 페이지 구성 [표 14 -1] 자료실 제작 실습에 사용하는 파일/폴더 목록
1.2 준비 파일과 페이지 구성 [그림 14-2] 자료실의 페이지와 기능 흐름
2. DB 테이블 설계 및 생성 [표 14-2] 자료실 데이터베이스 테이블(테이블명: download)
2. DB 테이블 설계 및 생성 [표 14-2] 자료실 데이터베이스 테이블(테이블명: download)
download 데이터베이스 테이블 생성 download.sql create table download ( num int not null auto_increment, id char(15) not null, name char(10) not null, nick char(10) not null, subject char(100) not null, content text not null, regist_daychar(20), hit int, file_name_0 char(40), file_name_1 char(40), file_name_2 char(40), file_name_3 char(40), file_name_4 char(40), file_copied_0 char(30), file_copied_1 char(30), file_copied_2 char(30), file_copied_3 char(30), file_copied_4 char(30), file_type_0 char(30), file_type_1 char(30), file_type_2 char(30), file_type_3 char(30), file_type_4 char(30), primary key(num) );
4.1 글 내용 보기 페이지 생성 [그림 14-5] 글 내용 보기 페이지
예제 14-1 자료실의 글 내용 보기 페이지 view.php 001 <? 002 session_start(); 003 include "../lib/dbconn.php"; 004 005 $sql="select * from $table where num=$num"; 006 $result=mysql_query($sql, $connect); 007 $row=mysql_fetch_array($result); 008 009 $item_num=$row[num]; 010 $item_id=$row[id]; 011 $item_name=$row[name]; 012 $item_nick=$row[nick]; 013 $item_hit=$row[hit]; 014 015 $file_name[0]=$row[file_name_0]; 016 $file_name[1]=$row[file_name_1]; 017 $file_name[2]=$row[file_name_2]; 018
예제 14-1 자료실의 글 내용 보기 페이지 view.php 019 $file_type[0]=$row[file_type_0]; 020 $file_type[1]=$row[file_type_1]; 021 $file_type[2]=$row[file_type_2]; 023 $file_copied[0=$row[file_copied_0]; 024 $file_copied[1=$row[file_copied_1]; 025 $file_copied[2=$row[file_copied_2]; 026 027 $item_date=$row[regist_day]; 028 $item_subject=str_replace(" ", " ", $row[subject]); 029 $item_content=str_replace(" ", " ", $row[content]); 030 $item_content=str_replace("\n", "<br>", $item_content); 031 $new_hit = $item_hit + 1; 032 033 $sql="update $table set hit=$new_hit where num=$num"; 034 mysql_query($sql, $connect); 035 ?> 036 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN“ "http://www.w3.org/TR/html4/loose.dtd">
예제 14-1 자료실의 글 내용 보기 페이지 view.php 037 <html> 038 <head> 039 <meta charset="euc-kr"> 040 <link href="../css/common.css" rel="stylesheet" type="text/css" media="all"> 041 <link href="../css/board3.css" rel="stylesheet" type="text/css" media="all"> 042 <script> 043 function del(href) 044 { 045 if(confirm("한번 삭제한 자료는 복구할 방법이 없습니다. \n\n정말 삭제하시겠습니까?")) 046 { 047 document.location.href=href; 048 } 049 } 050 </script> 051 </head> 052
예제 14-1 자료실의 글 내용 보기 페이지 view.php 053 <body> 054 <div id="wrap"> 055 056 <div id="header"> 057 <? include "../lib/top_login2.php"; ?> 058 </div> <!- - end of header - -> 059 060 <div id="menu"> 061 <? include "../lib/top_menu2.php"; ?> 062 </div> <!- - end of menu - -> 063 064 <div id="content"> 065 <div id="col1"> 066 <div id="left_menu"> 067 <? include "../lib/left_menu.php"; ?> 068 </div> 069 </div> <!- - end of col1 - -> 070
예제 14-1 자료실의 글 내용 보기 페이지 view.php 071 <div id="col2"> 072 <div id="title"> 073 <img src="../img/title_download.gif"> 074 </div> 075 <div id="view_comment"> </div> 076 <div id="view_title"> 077 <div id="view_title1"><?= $item_subject ?></div> <div id="view_title2"> <?=$item_nick ?>|조회 : <?=$item_hit ?>|<?=$item_date ?> </div> 078 </div> 079 080 <div id="view_content"> 081 <? 082 for($i=0; $i<3; $i++) 083 { 084 if($userid && $file_copied[$i]) 085 { 086 $show_name=$file_name[$i]; // 화면에 표시되는 파일명 087 $real_name=$file_copied[$i]; // [data] 폴더에 저장된 파일명
예제 14-1 자료실의 글 내용 보기 페이지 view.php 088 $real_type=$file_type[$i]; 089 $file_path="./data/".$real_name; 090 $file_size=filesize($file_path); 091 092 echo ("▷ 첨부파일 : $show_name ($file_size Byte) 093 <a href='download.php?table= $table&num=$num&real_name=$real_name&show_name= $show_name&file_type=$real_type'>[저장]</a><br>“ ); 094 } 095 } 096 ?> 097 <br> 098 <?= $item_content ?> 099 </div> 100
예제 14-1 자료실의 글 내용 보기 페이지 view.php 101 <div id="view_button"> 102 <a href="list.php?table=<?=$table?>&page=<?=$page?>"> <img src="../img/list.png"></a> 103 <? 104 if($userid && $userid==$item_id) 105 { 106 ?> 107 <a href="write_form.php?table=<?=$table?>&mode=modify& num=<?=$num?>&page=<?=$page?>"><img src="../img/modify.png"></a> 108 <a href="javascript:del('delete.php?table=<?=$table?>&num=<?=$num?>')"> <img src="../img/delete.png"></a> 109 <? 110 } 111 ?>
예제 14-1 자료실의 글 내용 보기 페이지 view.php 112 <? 113 if($userid) 114 { 115 ?> 116 <a href="write_form.php?table=<?=$table?>"><img src="../img/write.png"></a> 117 <? 118 } 119 ?> 120 </div> 121 <div class="clear"></div> 122 </div> <!- - end of col2 - -> 123 </div> <!- - end of content - -> 124 </div> <!- - end of wrap - -> 125 126 </body> 127 </html>
예제 14-2 첨부파일 다운로드 download.php 01 <? session_start(); ?> 02 <? 03 if(!$userid) 04 { 05 echo(" 06 <script> 07 window.alert('로그인 후 이용해 주세요.') 08 history.go(-1) 09 </script> 10 "); 11 exit; 12 } 13 $file_path = "./data/".$real_name; 14
예제 14-2 첨부파일 다운로드 download.php 15 if(file_exists($file_path)) 16 { 17 $fp=fopen($file_path,"rb"); 18 19 if($file_type) 20 { 21 Header("Content-type: application/x-msdownload"); 22 Header("Content-Length: ".filesize($file_path)); 23 Header("Content-Disposition: attachment; filename=$show_name"); 24 Header("Content-Transfer-Encoding: binary"); 25 Header("Content-Description: File Transfer"); 26 Header("Expires: 0"); 27 }
예제 14-2 첨부파일 다운로드 download.php 28 else 29 { 30 if(eregi("(MSIE 5.0|MSIE 5.1|MSIE 5.5|MSIE 6.0)", $HTTP_USER_AGENT)) 31 { 32 Header("Content-type: application/octet-stream"); 33 Header("Content-Length: ".filesize($file_path)); 34 Header("Content-Disposition: attachment; filename=$show_name"); 35 Header("Content-Transfer-Encoding: binary"); 36 Header("Expires: 0"); 37 } 38 else 39 { 40 Header("Content-type: file/unknown"); 41 Header("Content-Length: ".filesize($file_path)); 42 Header("Content-Disposition: attachment; filename=$show_name"); 43 Header("Content-Description: PHP3 Generated Data"); 44 Header("Expires: 0"); 45 } 46 }
예제 14-2 첨부파일 다운로드 download.php 47 if(!fpassthru($fp)) 48 fclose($fp); 49 } 50 ?>
Header() 함수 형식 void Header ( string $string ) - 인자: 웹 브라우저가 이해할 수 있는 약속된 헤더(문자열 형태) 기능 HTTP의 헤더를 클라이언트 측의 웹 브라우저에 보낸다. 반환값의 형식이 void이므로 반환값은 없으며, 헤더에 대한 자세한 내용은 PHP 공식 웹 사이트(php.net)를 참고하기 바란다. 사용예 <? Header('Content-type: application/pdf'); Header('Content-Disposition: attachment; filename="downloaded.pdf"'); ?> 2행은 전송하려는 파일의 형식이 pdf임을 알려주고, 3행에서는 전송하려는 파일명이 downloaded.pdf임을 클라이언트 측의 웹 브라우저에게 알려준다.
file_exists() 함수 형식 bool file_exists(string $filename) - 인자: 파일명이나 디렉터리명, 경로 기능 입력한 경로에 해당 파일이 있는지 확인한다. 파일이 존재하면 TURE를, 그렇지 않으면 FALSE를 반환한다.
fopen() 함수 형식 resource fopen(string $filename , string $mode) - 첫 번째 인자: 파일명과 파일이 존재하는 위치(경로) - 두 번째 인자: 읽기 모드 기능 파일을 열어서 파일이 있는 위치를 가르키는 파일 포인터를 반환한다. 두 번째 인자값에 따라 어떤 방법으로 파일을 여는지 결정된다. $mode에는 다음과 같은 값을 입력할 수 있다. - 'r' : 읽기 모드 - 'w' : 쓰기 모드 - 'b' : 이진(binary) 모드 사용예 <? $handle = fopen(".\folder\resource.exe", "rb"); ?> [folder] 폴더의 resource.exe 파일을 이진 읽기 모드로 열어서 파일 포인터($handle)에 저장한다. 이진 모드는 텍스트 파일이 아닌 실행파일(.exe)과 같은 일반 파일을 여는 데 사용한다.
fpassthru() 함수 • fclose() 함수 형식 int fpassthru(resource $handle) - 인자: 파일 포인터 기능 파일 포인터($handle)가 지시하는 현 위치에서 파일을 끝까지 읽어서 출력버퍼에 저장 형식 bool fclose(resource $handle) - 인자: 파일 포인터 기능 파일 포인터($handle)가 지시하는 파일을 닫는다. 함수를 실행하면 TRUE를, 실패하면 FALSE를 반환한다.