-
Blob - php/mysql 이미지 저장WEB 2021. 4. 7. 13:29
1. Blob란?
- binary large oabject 의 약자로 이진 파일을 의미한다.
- mysql 에서는 4가지 크기로 구분되며 이미지 ,텍스트 , 엑셀 , 음악등 다양한 파일을 저장할 수 있다.
- 예시: longblob 최대 4GB , medium 16mb ...
2. Post 사용예시
1. DB에 넣기
첫번째로 db 에 blob 형식을 갖고 있는 테이블을 만든다.
<form enctype="multipart/form-data" action="writePost.php" method="post"> <table width=1000px style="border: 1px solid gray;" cellpadding=5> <tr style="margin-top:20px ;"> <th> 사진 첨부 </th> <td> <input type="file" name="userfile" > </td> </tr> <tr style="margin-top:20px ;"> <td colspan="2"> <div style="text-align: center;"> <input type="submit" value="save"> </div> </td> </tr> </table> </form>
form 테그 안을 다음과같이 구성한다 중요한것은 enctype 과 input type 이다.
$image = addslashes(file_get_contents($_FILES['userfile']['tmp_name'])); //you keep your column name setting for insertion. I keep image type Blob. $query = "INSERT INTO picture (idx_board,file_data) VALUES('$idx_board','$image')"; $qry = mysqli_query($connect, $query);
앞에서 쏜 post를 post가 아닌 $_FILES 형태를 통해 받는것이 포인트다. 이렇게 받은 이미지 파일을 쿼리를 통해 mysql에 넣는다.
-addslahes 는 ''가 db에서 원치 않는 형태로 저장되지 않게끔하기 위해 사용함
-file_get_contents 는 $_FILES 를 통해 얻어낸 파일을 인자로 받고 파일을 읽어낸다. 이때 들어간 파일이 이미지 파일이므로 이진데이터를 뱉어낸다.
(일반적으로 설명서.txt 파일을 넣으면 해당 내용을 뱉어냄)
'WEB' 카테고리의 다른 글
JS 로 접근 디바이스 판별하기(모바일/PC 구분) (0) 2023.12.26 JavaScript 동작 방식 (Event Loop) (0) 2021.04.27