Transcript 자료실

4장장 자료실
15
한빛미디어(주)
Section
01
학습 목표
자료실의 동작 기능을 이해한다
자료실 게시판의 DB 테이블들의 구조를 이해한다
첨부 파일을 업로드하는 방법에 대하여 이해한다
첨부 파일에 관련된 정보를 DB에 저장하는 방법을 익힌다
첨부파일을 삭제하는 방법을 익힌다
첨부파일에 관련된 정보를 DB에서 가져와 출력하는 부분을 이해한
다.
2
Section
주요
학습01내용
01. 자료실 파일목록 및 DB 테이블
02. 자료실 목록 보기
03. 자료실 글 쓰기
04. 글내용 보기
05. 글 삭제
3
Section 01
자료실 미리 보기
 질의응답에 첨부파일 업로드 기능 추가
4
Section 02
01
자료실 파일 목록
파일명
설명
down_board.sql
공지사항 DB 테이블 생성 명령
down_ripple.sql
리플 저장을 위한 DB 테이블 생성 명령
list.php
자료실 목록 보기
insert.php
자료실 글을 DB에 저장
insert_ripple.php
리플을 DB에 저장
delete.php
게시판 글을 DB에서 삭제
delete_ripple.php
리플을 DB에서 삭제
modify_form.php
자료실 글 수정 양식
modify.php
자료실 글을 DB에서 수정
search.php
특정 문자열로 검색된 자료실 글 목록
view.php
글 내용 보기
write_form.php
자료실 글쓰기 양식
[표 15-1] 자료실 게시판에 사용되는 파일 목록
5
Section 03
01
자료실 프로그램 설치
⑴ 작업 폴더(“www" 폴더) 밑에 “download" 폴더를 만들고 생성된
“download" 폴더 밑에 교재 뒤 CD의 “www\download" 폴더에 있
는 파일들과 "img" 디렉토리를 통째로 복사한다.
⑵ 생성된 “download” 폴더 밑에 “data” 폴더를 만든다.
⑶ 두 개의 자료실 DB 테이블을 생성한다.
- 게시판 DB 테이블
- 리플 DB 테이블
6
Section 04
01
자료실 게시판 DB 테이블
필드 이름
타입
추가 사항
필드 설명
num
int
not null, auto_increment, primary key
일렬 번호
group_num
int
not null
그룹 번호
ord
int
not null
답변 글 순서
depth
int
not null
답변 글 깊이
id
varchar(10)
not null
아이디
name
varchar(10)
not null
이름
subject
varchar(100)
not null
제목
content
text
not null
글 내용
regist_day
varchar(20)
글 쓴 날짜
hit
int
조회수
ip
varchar(20)
접속 IP
filename
varchar(50)
파일 이름
filesize
varchar(20)
파일 크기
[표 15-2] 자료실의 게시판 DB 테이블 (테이블 명 : down_board)
7
Section 05
01
자료실 게시판 DB 테이블 만들기
“c:\mysql\bin” 폴더에 “down_board.sql” 파일 작성
CREATE TABLE down_board (
num int not null auto_increment,
group_num int not null,
ord int not null,
depth int not null,
id varchar(10) not null,
name varchar(10) not null,
subject varchar(100) not null,
content text not null,
regist_day varchar(20),
hit int,
ip varchar(20),
filename varchar(50),
filesize varchar(20),
PRIMARY KEY (num)
);
명령 프롬프트에서 다음을 실행
C:\mysql\bin> mysql -uphp5 -p1234 php5_db < down_board.sql
8
Section 06
01
자료실 게시판 DB 테이블
필드 이름
타입
추가 사항
필드 설명
num
int
not null, auto_increment,
primary key
일렬 번호
parent
int
not null
리플의 부모 글
id
varchar(10)
not null
아이디
name
varchar(20)
not null
이름
content
text
not null
리플 내용
regist_day
varchar(20)
리플 단 날짜
ip
varchar(20)
접속 IP
[표 15-3] 자료실 리플 DB 테이블 (테이블 명 : down_ripple)
9
Section 07
01
자료실 리플 DB 테이블 만들기
메모장으로 다음을 타이핑한 다음 “c:\mysql\bin” 폴더 밑에
“down_ripple.sql”이름으로 저장
CREATE TABLE down_ripple (
num int not null auto_increment,
parent int not null,
id varchar(10) not null,
name varchar(10) not null,
content text not null,
regist_day varchar(20),
ip varchar(20),
PRIMARY KEY (num)
);
명령 프롬프트에서 다음을 실행
C:\mysql\bin> mysql -uphp5 -p1234 php5_db < down_ripple.sql
10
Section15-1】list.php
01
【예제
02
자료실 목록 보기
108 :
109 :
111
112
113
114
116
117
:
:
:
:
:
:
181
182
183
184
:
:
:
:
if ($row[filename])
{ echo “ <td align=center><img src='img/file.gif‘
border=0></td>
<td align=center>$row[filesize]</td> ";
}
else
{ echo “ <td align=center></td>
<td align=center></td> ";
}
// 중간 생략
<td align=right>
<a href='write_form.php'>
<img src='img/i_write.gif' align=absmiddle border=0></a>
&nbsp;<a href="list.php"><img src="img/i_list.gif“
border=0></a>
11
Section15-2】write_form.php
01
【예제
02
자료실 글쓰기
21 :
22 :
23 :
24 :
25 :
26 :
97 :
98 :
117 :
<form name='writeform'
action='insert.php?num=<? echo $num ?>&page=<? Echo
$page ?>'
method='post' enctype='multipart/form-data'>
<table width=776 border=0 cellspacing=0 cellpadding=0
align=center>
<tr>
<td colspan="5" height=25>
<img src="img/down_title.gif"></td>
// 중간 생략
<td><input style='font-size:8pt;border:1px solid' type='file'
name='upfile' size=50 maxlength=100></td>
// 중간 생략
</form>
12
파일 업로드
Section 08
01
변수명
내용
$upfile
서버에 저장되는 임시 파일명
$upfile_name
실제 업로드한 파일명
$upfile_size
파일 크기(바이트)
[표 15-4] 파일 업로드 입력 양식에서 name 속성을 “upfile" 로 했을 때 전달되는 변수
13
Section15-3】insert.php
01
【예제
02
DB 테이블에 저장
39 : if($upfile_name)
40 : {
41 :
if ( file_exists("data/$upfile_name") ){
42 :
echo(" <script>
44 :
window.alert('선택한 파일과 동일한 이름이 존재합니다.');
45 :
history.go(-1) </script>
47 :
");
48 :
exit; }
50 :
51 :
if( !$upfile ) { echo(" <script>
54 :
window.alert
55 :
('업로드 파일 사이즈가 지정된 용량(2M)을 초과합니다.');
56 :
history.go(-1) </script>
58 :
");
59 :
exit; }
14
Section15-3】insert.php
01
【예제
02
62
63
65
67
69
70
71
72
73
74
75
76
77
78
79
80
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
if( strlen($upfile_size) < 7 )
$filesize = sprintf("%0.2f KB", $upfile_size/1000);
else
$filesize = sprintf("%0.2f MB", $upfile_size/1000000);
if( !copy($upfile, "data/$upfile_name") )
{
echo("
<script>
window.alert
('파일을 지정한 디렉토리에 복사하는데 실패했습니다.');
history.go(-1)
</script>
");
exit;
}
15
Section15-3】insert.php
01
【예제
02
82 :
if ( !unlink($upfile) ) {
83 :
echo("
84 :
<script>
85 :
window.alert('임시파일을 삭제하는데 실패했습니다.');
86 :
history.go(-1)
87 :
</script>
88 :
");
89 :
exit;
90 :
}
91 : }
92 :
93 : if (!$num)
94 : {
95 :
$depth = 0; // depth, ord 를 0으로 초기화
96 :
$ord = 0;
16
Section15-3】insert.php
01
【예제
02
98 :
99 :
100 :
101 :
102 :
103 :
104 :
105 :
106 :
107 :
// 레코드 삽입 명령
$sql = "insert into down_board
(depth, ord, id, name, subject,";
$sql .= "content, regist_day, hit, ip, filename, filesize) ";
$sql .= "values($depth, $ord, '$userid', '$username',
'$subject',";
$sql .= "'$content', '$regist_day', 0, '$ip', '$upfile_name',
'$filesize')";
mysql_query($sql, $connect); // $sql 에 저장된 명령 실행
// 중간 생략
118 :
119 :
120 :
122 :
123 :
124 :
}
else
{ // 부모 글 가져오기
$sql = "select * from down_board where num = $num";
$result = mysql_query($sql, $connect);
$row = mysql_fetch_array($result);
17
Section15-3】insert.php
01
【예제
02
131
132
133
134
135
136
137
138
139
140
:
:
:
:
:
:
:
:
:
:
142
143
144
146
147
:
:
:
:
:
$sql = "update down_board set ord = ord + 1
where group_num = $row[group_num] and ord > $row[ord]";
mysql_query($sql, $connect);
$sql = "insert into down_board(group_num, depth, ord, id, name,
subject,";
$sql .= "content, regist_day, hit, ip, filename, filesize) ";
$sql .= "values($group_num, $depth, $ord, '$userid', '$username',
'$subject',";
$sql .= "'$content', '$regist_day', 0, '$ip',
'$upfile_name','$filesize')";
mysql_query($sql, $connect); // $sql 에 저장된 명령 실행
}
mysql_close();
// DB 연결 끊기
Header("Location:list.php?page=$page"); // list.php 로 이동
?>
18
Section15-4】view.php
01
【예제
02
글 내용 보기
70 : <?
71 : if ($row[filename])
72 : {
73 :
echo "
74 :
<tr valign=center>
75 :
<td width=5></td>
76 :
<td><img src='img/file.gif'>
77 :
<a href='data/$row[filename]'>$row[filename]
78 :
($row[filesize])</a></td></tr>
79 :
<tr height=5 ><td colspan=2></td></tr>
80 :
";
81 : }
82 : ?>
19
Section15-5】delete.php
01
【예제
02
글 삭제
11 : if ($userid != "admin" and $userid != $row[id])
12 : { echo("
14 :
<script>
15 :
window.alert('삭제 권한이 없습니다.')
16 :
history.go(-1)
17 :
</script>
18 :
");
19 :
exit;
20 : }
21 : else
22 : { if ($row[filename])
25 :
unlink("data/$row[filename]");
26 :
28 :
$sql = "delete from down_board where num = $num";
29 :
mysql_query($sql, $connect);
30 : }
20