노력이 좋아서
<step47>'php_GET, POST, file_get_contents(), file_put_contents()'
zoaseo
2022. 5. 25. 12:33
1) $_GET
<!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 action="number.php" method="get">
<input type="number" name="num1" max="20" min="1">
<input type="number" name="num2" max="20" min="1">
<button type="submit">전송</button>
<button type="reset">취소</button>
</form>
</body>
</html>
<!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>
<?php
$num1 = $_GET['num1']; //html의 form태그 name속성에서 받아온다.
$num2 = $_GET['num2']; //html의 form태그 name속성에서 받아온다.
var_dump($_GET);
?>
<p>
<?php
if($num1 == $num2){
echo "num1의 값은 {$num1}이고 num2의 값은 {$num2}이며 같습니다.";
}elseif($num1 > $num2){
echo "num1의 값은 {$num1}이고 num2의 값은 {$num2}이며 num1이 더 큽니다.";
}else{
echo "num1의 값은 {$num1}이고 num2의 값은 {$num2}이며 num2가 더 큽니다.";
}
?>
</p>
</body>
</html>

2) $_POST
<!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 action="number.php" method="post">
<input type="number" name="num1" max="20" min="1">
<input type="number" name="num2" max="20" min="1">
<button type="submit">전송</button>
<button type="reset">취소</button>
</form>
</body>
</html>
<!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>
<?php
$num1 = $_POST['num1']; //html의 form태그 name속성에서 받아온다.
$num2 = $_POST['num2']; //html의 form태그 name속성에서 받아온다.
var_dump($_POST);
?>
<p>
<?php
if($num1 == $num2){
echo "num1의 값은 {$num1}이고 num2의 값은 {$num2}이며 같습니다.";
}elseif($num1 > $num2){
echo "num1의 값은 {$num1}이고 num2의 값은 {$num2}이며 num1이 더 큽니다.";
}else{
echo "num1의 값은 {$num1}이고 num2의 값은 {$num2}이며 num2가 더 큽니다.";
}
?>
</p>
</body>
</html>

3) file_get_contents()
index.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>
<h1>리빙페이지</h1>
<ul>
<li><a href="index.php?title=햇빛 좋은 어느 날, 우리 집&text=living1">햇빛 좋은 어느 날, 우리 집</a></li>
<li><a href="index.php?title=북한산 산세를 병풍처럼 두른 한옥마을 단독주택&text=living2">북한산 산세를 병풍처럼 두른 한옥마을 단독주택</a></li>
<li><a href="index.php?title=오래된 주택 셀프 리모델링, 주방타일에 힘 좀 썼습니다.&text=living3">오래된 주택 셀프 리모델링, 주방타일에 힘 좀 썼습니다.</a></li>
</ul>
<p>
<a href="write.php">파일추가하기</a>
</p>
<h2>
<?php
echo $_GET['title'];
?>
</h2>
<p>
<?php
echo file_get_contents("data/".$_GET['text']);
?>
</p>
</body>
</html>

4) file_put_contents()
write_process.php
<?php
//file_put_contents(파일명, 내용);
$title = $_POST['title'];
$description = $_POST['desc'];
file_put_contents('./data/'.$title, $description);
echo "파일이 추가되었습니다.";
?>
write.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 action="write_process.php" method="post" onsubmit="return formCheck()">
<input type="text" name="title" id="titleBox">
<input type="text" name="desc" id="descBox">
<button type="submit">전송</button>
<button type="reset">취소</button>
</form>
<script>
function formCheck(){
let titlebox = document.querySelector('#titleBox');
let descbox = document.querySelector('#descBox');
if(!titlebox.value || !descbox.value){
alert('제목과 내용을 입력해주세요!');
return false;
}
}
</script>
</body>
</html>





5) scandir()
write_process.php
<?php
//file_put_contents(파일명, 내용);
$title = $_POST['title'];
$description = $_POST['desc'];
file_put_contents('./data/'.$title, $description);
echo "파일이 추가되었습니다.";
//리다이렉션
header('Location:index.php');
?>
index.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>
<h1>리빙페이지</h1>
<ul>
<!-- <li><a href="index.php?title=햇빛 좋은 어느 날, 우리 집&text=living1">햇빛 좋은 어느 날, 우리 집</a></li>
<li><a href="index.php?title=북한산 산세를 병풍처럼 두른 한옥마을 단독주택&text=living2">북한산 산세를 병풍처럼 두른 한옥마을 단독주택</a></li>
<li><a href="index.php?title=오래된 주택 셀프 리모델링, 주방타일에 힘 좀 썼습니다.&text=living3">오래된 주택 셀프 리모델링, 주방타일에 힘 좀 썼습니다.</a></li> -->
<?php
$lists = scandir('data/');
for($i=0; $i<count($lists); $i++){ //$i<count($lists) 길이만큼 실행한다
$title = $lists[$i];
if($lists[$i] != "." && $lists[$i] != ".."){ //페이지 소스를 보면 첫번째 두번째를 제외시킨다.
// echo "<li><a href='index.php?title=".$title."'>".$title."</a></li>";
echo "<li><a href='index.php?title={$title}'>$title</a></li>";
}
}
?>
</ul>
<p>
<a href="write.php">파일추가하기</a>
</p>
<h2>
<?php
echo $_GET['title'];
?>
</h2>
<p>
<?php
echo file_get_contents("data/".$_GET['title']);
?>
</p>
</body>
</html>




6) ex
index.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>
<style>
body {
width: 100%;
max-width: 1200px;
margin: 0 auto;
}
#flex {
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid #ccc;
}
button {
background-color: salmon;
border: none;
color: #fff;
border-radius: 2px;
padding: 3px 13px;
}
a {
text-decoration: none;
color: inherit;
}
li {
list-style: none;
}
textarea {
display: none;
}
#main {
width: 220px;
height: 20px;
}
</style>
</head>
<body>
<div id="flex">
<h1>Blue Blog</h1>
<div>
<a href="index.php"><button id="home">홈</button></a>
<button id="write">글쓰기</button>
</div>
</div>
<ul>
<?php
$lists = scandir('data1/');
for($i=0; $i<count($lists); $i++){ //$i<count($lists) 길이만큼 실행한다
$title = $lists[$i];
if($lists[$i] != "." && $lists[$i] != ".."){ //페이지 소스를 보면 첫번째 두번째를 제외시킨다.
echo "<li><h3><a href='index.php?text={$title}'>$title</a></h3></li>";
}
}
?>
</ul>
<form action="write_process.php" method="get">
<textarea name="text" id="main" cols="30" rows="10" placeholder="제목"></textarea><br>
<textarea name="content" cols="30" rows="10" placeholder="내용"></textarea><br>
<button type="submit">등록</button>
<button id="remove" type="reset">삭제</button>
</form>
<div>
<h2>
<?php
echo $_GET['text'];
?>
</h2>
<p>
<?php
echo file_get_contents("data1/".$_GET['text']);
?>
</p>
</div>
<script>
document.querySelector('#home').addEventListener('click', function(){
document.querySelectorAll('textarea')[0].style.display = 'none';
document.querySelectorAll('textarea')[1].style.display = 'none';
})
document.querySelector('#write').addEventListener('click', function(){
document.querySelectorAll('textarea')[0].style.display = 'block';
document.querySelectorAll('textarea')[1].style.display = 'block';
})
// document.querySelector('#remove').addEventListener('click', function(e){
// target.document.querySelectorAll('li').style.display = 'none';
// })
</script>
</body>
</html>
write_process.php
<?php
//file_put_contents(파일명, 내용);
$title = $_GET['text'];
$description = $_GET['content'];
file_put_contents('data1/'.$title, $description);
echo "파일이 추가되었습니다.";
//리다이렉션
header('Location:index.php');
?>
