* 폼태그로 파일을 전송할 때
1) form에 속성추가
enctype = "multipart/form-data"
<input type="file" name="img">
2) 폼태그로 전송된 파일은 php 슈퍼글로벌 $_ FILES에 담겨있음
실제 저장되는 임시위치 $_FILES['img']['tmp_name']
3) 임시위치에서 원하는 위치로 파일을 업로드
move_uploaded_file(임시위치, 이동할위치);
ex16_imgupload.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!--
파일 전송시 form태그에 밑 속성 추가하기
enctype="multipart/form-data"
-->
<form action="ex16_img_process.php" method="post"
enctype="multipart/form-data">
이미지업로드: <input type="file" name="img"><button type="submit">확인</button>
</form>
</body>
</html>
ex16_img_process.php
<?php
$file=$_FILES['img'];
var_dump($file);
// 파일 업로드시 임시저장위치
echo $file['tmp_name'];
// 실제 저장하고 싶은 위치 C:Apache24/htdocs/php/
// 업로드된 파일을 내가 지정한 위치에 지정한 파일명으로 파일을 이동
// move_uploaded_file(현재위치, 이동할위치)
$result = move_uploaded_file($file['tmp_name'],'C:Apache24/htdocs/php/'.$file['name']);
if($result) {
?>
<img src="../<?=$file['name']?>">
<?php
}
?>
'Stack > PHP' 카테고리의 다른 글
[PHP] Book Blog(Books) 만들기 2_도서등록 / 자세히보기 / 수정 / 삭제 (0) | 2022.06.02 |
---|---|
[PHP] Book Blog(Books) 만들기_첫 화면 / 회원가입 / 로그인 / 로그아웃 / 검색 (0) | 2022.06.02 |
[PHP] 쿠키 / 세션 (0) | 2022.05.30 |
[PHP] MySQL 연동하기 (0) | 2022.05.26 |
[PHP] file 관련 함수를 이용한 Blog (0) | 2022.05.26 |