메뉴 건너뛰기

프로그램언어

조회 수 35886 추천 수 1 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
PHP] 게시판 글쓰기와 이미지 파일 DB 저장 및 불러오기 예제

게시판에 글쓰고, 이미지 파일 전송하여 DB에 저장하고, 게시물 볼때 게시물 내용과 이미지 파일 불러오는 예제입니다.
어느분이 물어보셔서 저장과 불러오기를 보여주기 위한 예제라 소스가 다소 엉성합니다~
개략적인 흐름만 참고하시고, 필요한 부분 덧붙여 사용하시면 될듯 합니다.

DB생성
- 게시판 글 내용 담을 테이블
CREATETABLE`inetag`.`test_bbs` (
 `bbsNo` INT NOTNULLAUTO_INCREMENTPRIMARYKEY ,
 `id` VARCHAR( 20)NOTNULL ,
 `content` VARCHAR( 255)NOTNULL ,
 `regdate` TIMESTAMP NOTNULLDEFAULT CURRENT_TIMESTAMP
) ENGINE=MYISAM ;

- 이미지 파일정보 담을 테이블
CREATETABLE`inetag`.`test_image` (
 `fileNo` INT NOTNULLAUTO_INCREMENTPRIMARYKEY ,
 `bbsNo` INT NOTNULL ,
 `path` VARCHAR( 255)NOTNULL ,
 `filename` VARCHAR( 50)NOTNULL
) ENGINE=MYISAM ;

소스
- form.php


<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>게시물 작성 예제 폼</title>
</head>
<body>
	<form enctype="multipart/form-data" name="form" method="post" action="write.php">
		<table>
		<tr>
			<td>아이디:</td>
			<td><input type="text" name="id" /></td>
		</tr>
		<tr>
			<td>내용:</td>
			<td><textarea name="content"></textarea></td>
		</tr>
		<tr>
			<td>이미지:</td>
			<td><input type="file" name="imageform" /></td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="submit" value="전송" />
			</td>
		</tr>
		</table>
	</form>
</body>
</html>



- write.php

<?
$id = $_POST['id'];
$content = $_POST['content'];

include "../include/db_connect.php";

$query = "insert into test_bbs (id,content) values('$id','$content')";

$db_inet->query($query);

$bbsid = $db_inet->insert_id;

//$path = $_SERVER['DOCUMENT_ROOT'].'/testBBS/';
$path = "/testBBS/";
$filename =  date("YmdHis").".jpg";
move_uploaded_file($_FILES['imageform']['tmp_name'], $filename);

$query = "insert into test_image (bbsNo,path,filename) values ($bbsid, '$path','$filename')";

$db_inet->query($query);

?>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>게시물 작성 예제 폼</title>
</head>
<body>
<table>
<tr>
	<td>전송아이디:</td>
	<td><?=$id;?></td>
</tr>
<tr>
	<td>전송내용:</td>
	<td><?=$content;?></td>
</tr>
<tr>
	<td>전송이미지</td>
	<td><img src="<?=$path.$filename;?>" /></td>
</tr>
</table>
<p><b>전송완료</b></p>
<p><a href='list.php'>목록가기</a></p>
</body>
</html>



- list.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?
include "../include/db_connect.php";
$result = $db_inet->query("select * from test_bbs");

echo "<table border=1>";
while($row = $result->fetch_assoc()) {
	echo "<tr>";
	echo "<td>".$row['bbsNo']."</td>";
	echo "<td>".$row['id']."</td>";
	echo "<td><a href='view.php?bbsno=".$row['bbsNo']."'>".$row['content']."</a></td>";
	echo "<td>".$row['regdate']."</td>";
	echo "</tr>";
}

?>



- view.php

<?
$bbsno = $_GET['bbsno'];
include "../include/db_connect.php";
$result = $db_inet->query("select * from test_bbs where bbsNo=".$bbsno);
$row = $result->fetch_assoc();

$result = $db_inet->query("select * from test_image where bbsNo=".$bbsno);
$row2 = $result->fetch_assoc();
?>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>게시물 보기</title>
</head>
<body>
<table>
<tr>
	<td>번호</td>
	<td><?=$row['bbsNo'];?></td>
</tr>
<tr>
	<td>아이디</td>
	<td><?=$row['id'];?></td>
</tr>
<tr>
	<td>내용</td>
	<td><?=$row['content'];?></td>
</tr>
<tr>
	<td>작성시간</td>
	<td><?=$row['regdate'];?></td>
</tr>
<tr>
	<td>이미지</td>
	<td>
<?
if(!empty($row2)) 
	echo "<img src='".$row2['path'].$row2['filename']."' />";
else 
	echo "이미지 없음";
?>
	</td>
</tr>
</table>
<p><b>전송완료</b></p>
<p><a href='list.php'>목록가기</a></p>
</body>
</html>


정말 간략한 예제입니다^^ 디자인에는 전혀 신경쓰지 않았으니 흐름만 참고하세여~


  1. php 문자열관련 함수

    Date2017.03.06 Views15580
    Read More
  2. php로 db 컨트롤 1

    Date2017.03.06 Views15769
    Read More
  3. PHP에서 Excel 파일을 만들 수 있는 PHPExcel

    Date2017.03.06 Views17112
    Read More
  4. PHP의 유동변수!? - $a1 ~ $a2 같은 형식의 변수를 반복문 돌릴때...

    Date2017.03.06 Views16610
    Read More
  5. PHP EXCEL export시 시트 이름 지정하여 여러 시트에 데이터 쓰기

    Date2017.03.06 Views18264
    Read More
  6. PHP 만년달력 소스

    Date2017.03.06 Views17063
    Read More
  7. text파일에 한줄씩 내용추가하기

    Date2017.03.06 Views17533
    Read More
  8. 지엠 웹에디터 v1.1 (저작권표시없음)|

    Date2017.03.06 Views17109
    Read More
  9. Ajax로 구연한 실시간 서버시간출력

    Date2017.03.06 Views21031
    Read More
  10. ajax refresh 시키기(자동리플래쉬) with php

    Date2017.03.06 Views23185
    Read More
  11. 오류 메시지 출력(alert) 및 페이지 이동(refresh) 관련

    Date2017.03.06 Views18568
    Read More
  12. 메일주소의 골뱅이를 그림처리하기

    Date2017.03.06 Views15508
    Read More
  13. PHP로 엑셀 자료 MySQL에 넣기

    Date2017.03.06 Views17875
    Read More
  14. CodeIgniter에서 PHPExcel 사용하기

    Date2017.03.06 Views20289
    Read More
  15. PHPExcel 클래스를 이용해 Excel 2007~2010 의 xlsx 파일 읽기 (100만 행 까지)

    Date2017.03.06 Views21694
    Read More
  16. phpexcel을 이용한 PHP로 엑셀파일 읽기와 생성

    Date2017.03.06 Views22787
    Read More
  17. 파일업로드

    Date2017.02.19 Views19352
    Read More
  18. PHP에서 데이터를 엑셀(Excel)로 저장

    Date2017.02.19 Views18428
    Read More
  19. [PHP] 게시판 글쓰기와 이미지 파일 DB 저장 및 불러오기 예제

    Date2017.02.19 Views35886
    Read More
  20. array_slice 배열의 일부를 추출

    Date2016.12.23 Views20775
    Read More
Board Pagination Prev 1 ... 6 7 8 9 10 11 12 13 14 15 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved