내용관리에 첨부파일 기능을 넣으려면 어디를 수정해야 할까요?
본문
내용관리 부분에 첨부파일 기능을 추가하려고 합니다.
게시판처럼 복잡하게 가변성으로 변하는 첨부파일은 구조가 어려울듯 하고 내용관리 안에 있는 것처럼 상단이미지 / 하단 이미지처럼 고정된 몇개의 첨부파일 버튼을 만들려고 하는데 어디에 어떤 코드를 수정해야 하는지 궁금합니다.
adm에 contentform.php랑 contentformupdate.php 파일만 수정하면 되는지 별도로 추가할 코드는 어떤것인지 문의합니다.
게시판 첨부파일과 내용관리 첨부파일은 코드가 다른듯 합니다.
답변 1
1. contentform.php 수정사항
데이터베이스 필드 추가 (기존 필드 체크 부분 다음에)
// 첨부파일명 필드 추가
if (!sql_query(" select co_file1_name from {$g5['content_table']} limit 1 ", false)) {
$sql = " ALTER TABLE `{$g5['content_table']}`
ADD `co_file1_name` VARCHAR( 255 ) NOT NULL DEFAULT '',
ADD `co_file2_name` VARCHAR( 255 ) NOT NULL DEFAULT '' ";
sql_query($sql, false);
}
기본값 배열 수정 (기존 $co 배열에 추가)
} else {
$html_title .= ' 입력';
$co = array(
'co_id' => '',
'co_subject' => '',
'co_content' => '',
'co_mobile_content' => '',
'co_include_head' => '',
'co_include_tail' => '',
'co_tag_filter_use' => 1,
'co_html' => 2,
'co_skin' => 'basic',
'co_mobile_skin' => 'basic',
'co_file1_name' => '',
'co_file2_name' => ''
);
}
첨부파일 필드 추가 (상단/하단 이미지 다음에)
<tr>
<th scope="row"><label for="co_file1">첨부파일1</label></th>
<td>
<input type="file" name="co_file1" id="co_file1">
<?php
$file1_path = G5_DATA_PATH . '/content/' . $co['co_id'] . '_file1';
if (file_exists($file1_path) && $co['co_file1_name']) {
echo '<div class="file_info">';
echo '<a href="'. G5_BBS_URL . '/content_download.php?co_id=' . $co['co_id'] . '&file=file1" target="_blank">' . htmlspecialchars($co['co_file1_name']) . '</a>';
echo ' <input type="checkbox" name="co_file1_del" value="1" id="co_file1_del"> <label for="co_file1_del">삭제</label>';
echo '</div>';
}
?>
</td>
</tr>
<tr>
<th scope="row"><label for="co_file2">첨부파일2</label></th>
<td>
<input type="file" name="co_file2" id="co_file2">
<?php
$file2_path = G5_DATA_PATH . '/content/' . $co['co_id'] . '_file2';
if (file_exists($file2_path) && $co['co_file2_name']) {
echo '<div class="file_info">';
echo '<a href="'. G5_BBS_URL . '/content_download.php?co_id=' . $co['co_id'] . '&file=file2" target="_blank">' . htmlspecialchars($co['co_file2_name']) . '</a>';
echo ' <input type="checkbox" name="co_file2_del" value="1" id="co_file2_del"> <label for="co_file2_del">삭제</label>';
echo '</div>';
}
?>
</td>
</tr>
2. contentformupdate.php 수정사항
첨부파일 삭제 변수 추가 (기존 이미지 삭제 변수 다음에)
$co_file1_del = (isset($_POST['co_file1_del']) && $_POST['co_file1_del']) ? 1 : 0;
$co_file2_del = (isset($_POST['co_file2_del']) && $_POST['co_file2_del']) ? 1 : 0;
첨부파일 삭제 처리 (기존 이미지 삭제 처리 다음에)
if ($co_file1_del) {
@unlink(G5_DATA_PATH . "/content/{$co_id}_file1");
}
if ($co_file2_del) {
@unlink(G5_DATA_PATH . "/content/{$co_id}_file2");
}
SQL 공통 부분 수정 (파일명 필드 추가)
$co_file1_name = '';
$co_file2_name = '';
// 기존 파일명 유지 (삭제되지 않은 경우)
if ($w == "u") {
$co_file1_name = $co_file1_del ? '' : $co_row['co_file1_name'];
$co_file2_name = $co_file2_del ? '' : $co_row['co_file2_name'];
}
// 새 파일 업로드시 파일명 업데이트
if ($_FILES['co_file1']['name']) {
$co_file1_name = $_FILES['co_file1']['name'];
}
if ($_FILES['co_file2']['name']) {
$co_file2_name = $_FILES['co_file2']['name'];
}
$sql_common = " co_include_head = '$co_include_head',
co_include_tail = '$co_include_tail',
co_html = '$co_html',
co_tag_filter_use = '$co_tag_filter_use',
co_subject = '$co_subject',
co_content = '$co_content',
co_mobile_content = '$co_mobile_content',
co_seo_title = '$co_seo_title',
co_skin = '$co_skin',
co_mobile_skin = '$co_mobile_skin',
co_file1_name = '$co_file1_name',
co_file2_name = '$co_file2_name' ";
첨부파일 업로드 처리 (기존 이미지 업로드 처리와 함께)
if ($w == "" || $w == "u") {
// 기존 이미지 처리 코드...
if ($_FILES['co_himg']['name']) {
$dest_path = G5_DATA_PATH . "/content/" . $co_id . "_h";
@move_uploaded_file($_FILES['co_himg']['tmp_name'], $dest_path);
@chmod($dest_path, G5_FILE_PERMISSION);
}
if ($_FILES['co_timg']['name']) {
$dest_path = G5_DATA_PATH . "/content/" . $co_id . "_t";
@move_uploaded_file($_FILES['co_timg']['tmp_name'], $dest_path);
@chmod($dest_path, G5_FILE_PERMISSION);
}
// 첨부파일 처리 추가
if ($_FILES['co_file1']['name']) {
$dest_path = G5_DATA_PATH . "/content/" . $co_id . "_file1";
@move_uploaded_file($_FILES['co_file1']['tmp_name'], $dest_path);
@chmod($dest_path, G5_FILE_PERMISSION);
}
if ($_FILES['co_file2']['name']) {
$dest_path = G5_DATA_PATH . "/content/" . $co_id . "_file2";
@move_uploaded_file($_FILES['co_file2']['tmp_name'], $dest_path);
@chmod($dest_path, G5_FILE_PERMISSION);
}
// 기존 에러 처리 및 리다이렉트 코드...
}
삭제 시 첨부파일도 함께 삭제 (기존 이미지 삭제와 함께)
} elseif ($w == "d") {
@unlink(G5_DATA_PATH . "/content/{$co_id}_h");
@unlink(G5_DATA_PATH . "/content/{$co_id}_t");
// 첨부파일 삭제 추가
@unlink(G5_DATA_PATH . "/content/{$co_id}_file1");
@unlink(G5_DATA_PATH . "/content/{$co_id}_file2");
$sql = " delete from {$g5['content_table']} where co_id = '$co_id' ";
sql_query($sql);
run_event('admin_content_deleted', $co_id);
}
3. content_download.php 파일 생성
bbs 폴더에 `content_download.php` 파일을 새로 생성:
<?php
include_once './_common.php';
$co_id = isset($_GET['co_id']) ? preg_replace('/[^a-z0-9_]/i', '', $_GET['co_id']) : '';
$file = isset($_GET['file']) ? $_GET['file'] : '';
if (!$co_id || !$file) {
die('잘못된 요청입니다.');
}
// 허용된 파일명만 처리
$allowed_files = array('file1', 'file2');
if (!in_array($file, $allowed_files)) {
die('허용되지 않은 파일입니다.');
}
// 데이터베이스에서 파일 정보 조회
$sql = " select co_file1_name, co_file2_name from {$g5['content_table']} where co_id = '$co_id' ";
$row = sql_fetch($sql);
if (!$row) {
die('해당 내용이 존재하지 않습니다.');
}
$file_path = G5_DATA_PATH . '/content/' . $co_id . '_' . $file;
if (!file_exists($file_path)) {
die('파일이 존재하지 않습니다.');
}
// 원본 파일명 가져오기
$original_name = '';
if ($file == 'file1' && $row['co_file1_name']) {
$original_name = $row['co_file1_name'];
} elseif ($file == 'file2' && $row['co_file2_name']) {
$original_name = $row['co_file2_name'];
}
if (!$original_name) {
die('파일명 정보가 없습니다.');
}
// 파일 다운로드
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $original_name . '"');
header('Content-Length: ' . filesize($file_path));
// 파일 출력
readfile($file_path);
exit;
?>
답변을 작성하시기 전에 로그인 해주세요.