웹마스터 팁

http://www.koreaphp.co.kr/tip_board/board_read.php?page=2&block=1&no=61&cate=43&parent=61- DB에 File을 저장
BLOB type을 이용하여 DB에 직접 저장

- Server의 지정된 Directory에 저장
파일의 위치와 이름만 DB에 저장
별도의 입출력 루틴이 필요하지 않다는 장점
파일의 수가 많아지면 관리가 힘듬
File의 실제 위치와 DB의 정보 불일치 가능성
File의 이름 충돌

- 디비 구조
CREATE TABLE gallery(
id INT NOT NULL auto_increment, image BLOB NOT NULL,
title VARCHAR(100) DEFAULT '' NOT NULL,
width SMALLINT(6) DEFAULT '0' NOT NULL,
height SMALLINT(6) DEFAULT '0' NOT NULL, file_size INT , # 파일크기
PRIMARY KEY (id)
) ;


- 이미지 받아들이는 HTML 소스

<form action='gallery.html' method='POST' enctype='multipart/form-data'>
<INPUT TYPE=hidden name=mode value=insert>
<TABLE>
<TR> <TD>올릴 이미지:</TD>
<TD><input type='file' name='image'></TD></TR>
<TR> <TD>제목</TD>
<TD><input type='text' name='title'></TD></TR>
<TR> <TD colspan = 2>
<input type='submit' value='이미지 전송 '></TD></TR>
</TABLE>
</form>


- 이미지를 저장하는 php 소스

$size = GetImageSize($image);
$width = $size[0];
$height = $size[1];
$imageblob = addslashes(fread(fopen($image, "r"), filesize($image)));
$filesize = filesize($image) ;

$query=" INSERT INTO gallery VALUES ('', '$imageblob', '$title', '$width','$height', '$filesize', '$detail' )" ;
$result=mysql_query($query,$connect );


- 이미지 리스트 보여주는 소스

$query= "select id, title, width, height from  gallery order by id DESC " ;
$result=mysql_query($que1,$connect );
$row=mysql_fetch_array($result);
 echo( "<table bordr=1 width=90% align=center>
<tr> <td>이미지</td>
  <td>제목</td>  
</tr>
");

while($row){
    echo ( "<tr><td><img src=./view.html?id=$row[id]
width=$row[width] height=$row[height] ></td>
<td>$row[title]</td> ");
  $row=mysql_fetch_array($result);
  }
  echo( "</table>");
}


- 이미지 보여주는 소스

<?php
$connect=mysql_connect(  "localhost",  "계정명",  "비번");
  mysql_select_db( "디비명",$connect);
$query= "select * from gallery where id=$id" ;
  $result=mysql_query($query,$connect );
$row=mysql_fetch_array($result);

  Header(  "Content-type:  image/jpeg");
  echo $row[image];
mysql_close();
?>