<?php
$i=$_GET['idx'];
$conn=mysqli_connect('', '', '', '');
$sql="SELECT filename, filecontent, mime FROM uploadt WHERE idx = ? ";
$stmt=$conn->prepare($sql);
$stmt->bind_param('i', $i);
$stmt->execute();
$stmt->bind_result($filename,$filecontent,$mime);
$stmt->fetch();
header('Content-Type: ' . $mime);
header('Content-Disposition: inline; filename="' . $filename . '"');
echo $filecontent;
?>
기존의 이미지 png말고 다른파일도 저장할 수 있게 변환
<?php
$i=$_GET['good'];
$conn=mysqli_connect('localhost','root','123456789a','sqli');
$sql="SELECT * FROM uploadt WHERE idx =$i";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_assoc($result);
if($row){
$filename=$row['filename'];
$filecontent=$row['filecontent'];
$mime=$row['mime'];
header("Content-Type: " . $mime);
header("Content-Disposition: attachment; filename=\"$filename\"");
echo $filecontent;
echo "<script>history.back(1);</script>";
}
?>
파일의 데이터 타입을 저장하기위해 함
<?php
$title=$_POST['title'];
$content=$_POST['content'];
$filecontent=file_get_contents($_FILES['bfile']['tmp_name']);// 파일 내용
$filename=$_FILES['bfile']['name']; // 파일 이름
$mime=$_FILES['bfile']['type']; //파일 데이터 타입
$conn=mysqli_connect('','','','');
$sql= "INSERT INTO uploadt (title, content, filename, filecontent, mime) VALUES (?, ?, ?, ?, ?)"
$stmt = $conn->prepare($sql);
$n=NULL;
$stmt->bind_param('sssbs',$title,$content,$filename,$n,$mime);
$stmt->send_long_data(3,$filecontent);
if($stmt->execute()){ ?>
<a href="./index.php">go</a>
<?php }else{
echo "fail". $stmt->error;
}
?>
$filecontent=file_get_contents($_FILES['bfile']['tmp_name']);
파일 내용(file_get_contents 때문에 지정된 파일을 문자열로 읽어옴)
$filename=$_FILES['bfile']['name'];
파일 이름
$mime=$_FILES['bfile']['type'];
파일 데이터 타입
$stmt->bind_param('sssbs',$title,$content,$filename,$n,$mime);
sssbs의 뜻은
| 단어 | 뜻 |
|---|---|
s | 문자열 타입을 의미 |
i | 정수 타입을 의미 |
d | 실수(더블) 타입을 의미 |
b | 바이너리 데이터(블롭) 타입을 의미 |
$stmt->send_long_data(3,$filecontent);
send_long_data()은 큰 데이터를 쿼리로 전송할때 사용
| 업로드 |
|---|
![]() |
MIME가 application/octet-stream로 변경되어 다운로드됨
| 조회 |
|---|
![]() |
| 결과 |
|---|
![]() |
중간에 가로채고 text/php로 바꾼후 실행
| 수정 | 결과 |
|---|---|
![]() | ![]() |
코드가 실행되지 않고 그대로 출력
| 다운로드 |
|---|
![]() |