개발 순서
게시글 보기 페이지에 '파일 다운로드' 기능 버튼 추가 > 파일 다운로드 처리할 실질적인 페이지 개발
1. 기존의 member_view.php 페이지에 파일 다운로드 버튼 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
<?php
$num = $_GET["num"];
$conn = mysqli_connect("localhost", "", "", "");
$sql = "select * from memberboard where board_num=$num";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$title = $row["title"];
$username = $row["username"];
$date = $row["wr_date"];
$content = $row["content"];
$content = str_replace(" ", " ", $content); // 공백 변환
$content = str_replace("\n", "<br>", $content); // 줄바꿈 변환
// 추가된 코드
$file_name = $row["file_name"];
$file_type = $row["file_type"];
$file_copied = $row["file_copied"];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="member1.css">
<title>회원 게시판</title>
</head>
<body>
<h2> 회원 게시판 > 내용보기</h2>
<ul class="board_form">
<li>
<span class="col1">ID</span>
<span class="col2"><?=$username?></span>
</li>
<li>
<span class="col1">제목 / 작성일</span>
<span class="col2"><?=$title?> / <?=$date?></span>
</li>
<li>
<span class="col1">내용</span>
<div class="content"><?= $content ?></div>
</li>
<!-- 추가된 코드 -->
<li>
<span class="col1"> 첨부 파일</span>
<span class="col2">
<?php
if($file_name) {
$file_path = "./file/".$file_copied;
$file_size = filesize($file_path);
echo "$file_name ($file_size Byte)
<a href='member_download.php?num=$num&file_copied=$file_copied&file_name=$file_name&file_type=$file_type'>[저장]</a><br><br>";
}
?>
</span>
</li>
<div class="buttons">
<button onclick="location.href='member_list.php'">목록보기</button>
<button onclick="location.href='member_modify_form.php?num=<?=$num?>'">수정하기</button>
<button onclick="location.href='member_delete.php?num=<?=$num?>'">삭제하기</button>
</div>
</ul>
</body>
</html>
|
cs |
19~21번, 48~61번 줄이 추가된 코드이다. [저장]을 클릭하면 member_download.php를 통해 저장한다.
2. 파일 다운로드 구현
member_download.php 페이지 소스코드.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?php
$file_copied = $_GET["file_copied"];
$file_name = $_GET["file_name"];
$file_type = $_GET["file_type"];
$file_path = "./file/".$file_copied;
if(file_exists($file_path)) {
header("Content-Type: application/octet-stream");
header('Content-Description: File Transfer');
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Transfer-Encoding:binary");
header("Cache-Control:cache,must-revalidate");
header("Pragma: public");
header("Content-Length: " . filesize($file_path));
flush();
readfile($file_path);
die();
}
?>
Colored by Color Scripter |
cs |
header() 함수를 통해 다음과 같이 다운로드 설정을 한다.
Content-Type: application/octet-stream: 전송될 데이터가 바이너리 형식임.
Content-Description: File Transfer: 전송될 데이터의 설명을 제공.
Content-Disposition: attachment; filename=$file_name: 다운로드 시 파일 이름 지정.
Content-Transfer-Encoding: binary: 데이터의 전송 인코딩을 바이너리 형식으로 설정.
Cache-Control: cache, must-revalidate: 캐시 제어 및 갱신을 설정.
Pragma: public: 캐시 제어 설정
Content-Length: filesize($file_path): 전송될 파일의 크기를 지정.
이후 flush() 함수로 출력 버퍼를 비우고, readfile($file_path) 함수를 통해 파일의 내용을 읽어 클라이언트에게 전송한다.
'Hacking_study > 개발과제' 카테고리의 다른 글
9주차_(1) 날짜 지정 검색 (0) | 2023.06.09 |
---|---|
8주차_(3) 조회수 (0) | 2023.06.05 |
8주차_(1) 파일 업로드 (0) | 2023.06.03 |
7주차 게시판 페이징 (0) | 2023.06.01 |
6주차_(3) 회원 게시판 글 검색 (0) | 2023.05.31 |