<!-- board_content_edit.php-->
<?php
require('dblogin_board.php');
require_once('logined_check_one.php');
$title = '';
$content = '';
$file_path = ''; // 파일 경로 초기화
if (isset($_GET['board_idx'])) {
$board_idx = $_GET['board_idx'];
// 해당 게시물의 내용을 데이터베이스에서 가져오기
$sql = "SELECT * FROM board_table WHERE board_idx = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $board_idx);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$title = htmlspecialchars($row['board_title']);
$content = htmlspecialchars($row['board_content']);
$file_path = $row['board_file_path']; // 파일 경로 가져오기
// 파일 이름 추출
$file_name = basename($file_path);
} else {
echo "게시물이 존재하지 않습니다.";
exit();
}
} else if (isset($_POST['board_idx'])) {
$board_idx = $_POST['board_idx'];
} else {
echo "게시물 인덱스가 전달되지 않았습니다.";
exit();
}
// 파일 삭제 요청 처리
if (isset($_POST['delete_file'])) {
$board_idx = $_POST['board_idx'];
$sql = "SELECT board_file_path FROM board_table WHERE board_idx = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $board_idx);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$file_path = $row['board_file_path'];
if (file_exists($file_path)) {
unlink($file_path); // 파일 삭제
$sql = "UPDATE board_table SET board_file_path = NULL WHERE board_idx = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $board_idx);
$stmt->execute();
header("Location: board_content_edit.php?board_idx=" . $board_idx);
exit();
} else {
echo "파일이 존재하지 않습니다.";
}
}
}
// 게시물 수정 요청 처리
if (isset($_POST['submit'])) {
$board_idx = $_POST['board_idx'];
$title = $_POST['title'];
$content = $_POST['content'];
$uploadOk = 1;
$target_file = $file_path;
// 파일 업로드 처리
if (!empty($_FILES["file"]["name"])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$fileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// 파일 이미 존재하는지 확인
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// 파일 크기 확인
if ($_FILES["file"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// 허용된 파일 형식 확인
$allowedTypes = array("jpg", "png", "jpeg", "gif", "pdf", "txt");
if (!in_array($fileType, $allowedTypes)) {
echo "Sorry, only JPG, JPEG, PNG, GIF, TXT, & PDF files are allowed.";
$uploadOk = 0;
}
// 파일 업로드 시도
if ($uploadOk == 1) {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file " . basename($_FILES["file"]["name"]) . " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
$uploadOk = 0;
}
} else {
$target_file = $file_path; // 업로드 실패 시 기존 파일 경로 유지
}
}
// 게시글 데이터 업데이트
if ($uploadOk == 1) {
$sql = "UPDATE board_table SET board_title = ?, board_content = ?, board_file_path = ? WHERE board_idx = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sssi", $title, $content, $target_file, $board_idx);
if ($stmt->execute()) {
echo "Record updated successfully";
header("Location: board_content.php?board_idx=" . $board_idx);
exit();
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$stmt->close();
$conn->close();
}
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<!-- 필요한 메타 태그와 스타일 시트, 제목 등 추가 -->
<link href="./style.css" rel="stylesheet" type="text/css" />
<meta charset="UTF-8">
</head>
<body>
<div class="container-content">
<h2>글 수정</h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="board_idx" value="<?php echo $board_idx; ?>">
<label for="title">제목:</label><br>
<input type="text" id="title" name="title" style="width:100%" value="<?php echo $title; ?>" required><br>
<label for="content">내용:</label><br>
<textarea id="content" name="content" style="width:100%; height:50%;" required><?php echo $content; ?></textarea><br>
<!-- 첨부 파일 표시 및 삭제 -->
<?php if (!empty($file_path)): ?>
<p>첨부 파일: <a href="<?php echo htmlspecialchars($file_path); ?>" download><?php echo htmlspecialchars($file_name); ?></a></p>
<button type="submit" name="delete_file">첨부 파일 삭제</button><br>
<?php endif; ?>
<label for="file">새 파일 업로드:</label><br>
<input type="file" name="file" id="file"><br><br>
<button type="submit" name="submit">수정 완료</button>
</form>
</div>
</body>
</html>