111 lines
3.1 KiB
PHP
111 lines
3.1 KiB
PHP
<?php
|
|
session_start();
|
|
header('Content-Type: application/json');
|
|
|
|
if (empty($_SESSION['login'])) {
|
|
echo json_encode(['status'=>'error','message'=>'로그인 필요']);
|
|
exit;
|
|
}
|
|
|
|
require_once __DIR__.'/db_conn.php';
|
|
|
|
$postId = (int)($_POST['postId'] ?? 0);
|
|
$content = trim($_POST['comment'] ?? '');
|
|
|
|
if ($postId < 1 || ($content === '' && empty($_FILES['images']))) {
|
|
echo json_encode(['status'=>'error','message'=>'내용 없음']);
|
|
exit;
|
|
}
|
|
|
|
$user = $_SESSION['login'];
|
|
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
/* =========================
|
|
1) 댓글 INSERT
|
|
========================= */
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO kngil.qa_comments
|
|
(post_id, commenter, user_nm, content, cdt_dt)
|
|
VALUES
|
|
(:post_id, :commenter, :user_nm, :content, NOW())
|
|
RETURNING comment_id
|
|
");
|
|
$stmt->execute([
|
|
':post_id' => $postId,
|
|
':commenter' => $user['user_id'],
|
|
':user_nm' => $user['user_nm'],
|
|
':content' => $content
|
|
]);
|
|
|
|
$commentId = $stmt->fetchColumn();
|
|
|
|
/* =========================
|
|
2) 이미지 업로드
|
|
========================= */
|
|
$images = [];
|
|
|
|
if (!empty($_FILES['images']['name'][0])) {
|
|
|
|
$uploadDir = $_SERVER['DOCUMENT_ROOT'].'/kngil/uploads/comment/';
|
|
if (!is_dir($uploadDir)) {
|
|
mkdir($uploadDir, 0777, true);
|
|
}
|
|
|
|
foreach ($_FILES['images']['name'] as $i => $name) {
|
|
|
|
if ($_FILES['images']['error'][$i] !== UPLOAD_ERR_OK) continue;
|
|
|
|
$tmp = $_FILES['images']['tmp_name'][$i];
|
|
$size = $_FILES['images']['size'][$i];
|
|
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
|
|
|
$save = time().'_'.bin2hex(random_bytes(4)).'.'.$ext;
|
|
$fullPath = $uploadDir.$save;
|
|
move_uploaded_file($tmp, $fullPath);
|
|
|
|
$path = '/kngil/uploads/comment/'.$save;
|
|
|
|
// 🔥 DB 구조에 맞게 INSERT
|
|
$pdo->prepare("
|
|
INSERT INTO kngil.qa_comment_images
|
|
(comment_id, file_name, file_path, thumb_path, file_size, uploaded_at)
|
|
VALUES
|
|
(:comment_id, :file_name, :file_path, :thumb_path, :file_size, NOW())
|
|
")->execute([
|
|
':comment_id' => $commentId,
|
|
':file_name' => $name,
|
|
':file_path' => $path,
|
|
':thumb_path' => $path, // 지금은 동일 (추후 썸네일 분리 가능)
|
|
':file_size' => $size
|
|
]);
|
|
|
|
$images[] = [
|
|
'thumb' => $path,
|
|
'full' => $path,
|
|
'name' => $name
|
|
];
|
|
}
|
|
}
|
|
|
|
$pdo->commit();
|
|
|
|
echo json_encode([
|
|
'status' => 'ok',
|
|
'comment_id' => $commentId,
|
|
'comment_text' => $content,
|
|
'user_name' => $user['user_nm'],
|
|
'login_id' => $user['user_id'],
|
|
'created_at' => date('Y-m-d H:i'),
|
|
'images' => $images
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
$pdo->rollBack();
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => $e->getMessage()
|
|
]);
|
|
}
|