Stack/PHP

[PHP] file

7ingout 2022. 5. 25. 14:44

1. 파일 내용 불러오기

file_get_contents('파일경로');

파일에 내용을 반환

 

2. 파일 생성하기(내용 변경)

file_put_contents(파일명, 내용);

파일에 새로운 내용을 넣기

 

3. 파일 삭제하기

unliknk('파일경로');

 

4. 파일 이름 변경하기

rename(파일이름, 변경할이름);

ex> rename('abc','green');

 

5. 디렉토리 제어

scandir('파일경로')

 


 

http://localhost/php/file/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>
    <!-- <?php
        var_dump(scandir('data/'));
    ?> -->
    <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++) {
                $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
            if(isset($_GET[title])) {
                echo $_GET['title'];
            } else {
                echo "환영합니다.";
            }
           
        ?>
    </h2>
    <p>
        <?php
            // echo file_get_contents("data/living1");
            if(isset($_GET['title'])) {
                echo file_get_contents("data/".$_GET['title']);
            } else {
                echo "하이하이";
            }
        
        ?>
    </p>
</body>
</html>

 

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>

 

write_process.php

post 방식
get 방식

<?php
    // file_put_contents(파일명, 내용);
    // http://localhost/php/file/write_process.php?title=추가할제목&desc=내용입니다.
    $title = $_POST['title'];   // $_GET
    $description = $_POST['desc'];  // $_GET
   
    file_put_contents('./data/'.$title, $description);
    echo "파일이 추가되었습니다.";

    // 리다이렉션
    header('Location:index.php');
?>

 

 

 

내가 추가한 항목도 끼워짐 ~

 


 

http://localhost/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>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box }
        li { list-style: none; }
        a { text-decoration: none; color: inherit }
        body {
            background-color: #eee;
            font-size: 14px;
        }
        #wrap {
            margin: 50px auto;
            width: 100%;
            max-width: 1200px;
            background-color: #fff;
            height: 860px;
            padding: 0 40px;
        }
        header {
            width: 100%;
            display: flex;
            height: 80px;
            border-bottom: 1px solid #aaa;
            align-items: center;
            padding: 0 10px;
        }
        h1 {
            width: 90%;
            margin: 0 10px;
        }
        header ul {
            width: 10%;
            display: flex;
            justify-content: space-between;
        }
        section {
            padding: 20px 20px;
        }
        header li button {
            border: none;
            background-color: #fff;
            font-size: 14px;
            cursor: pointer;
        }
        #main {
            margin: 20px 0;
            display: none;
        }
        form {
            display: flex;
            flex-direction: column;
            width: 100%;
            padding: 0px 3px;
        }
        #main button {
            width: 60px;
            background-color: #fff;
            border: none;
        }
        #text_title {
            height: 20px;
            margin-bottom: 5px;
        }
        #content {
            padding: 50px 0;
            height: 150px;
        }
        #content_list li{
            padding: 5px 0;
        }
        #content p {
            padding: 10px 0;
        }
        #btn {
            text-align: right;
            padding: 5px 0;
        }
        #home {
            height: 300px;
            padding: 80px 20px; 
            background-image: linear-gradient(120deg, #e0c3fc 0%, #8ec5fc 100%);
            /* background-image: linear-gradient(to top, #a18cd1 0%, #fbc2eb 100%); */
            border-radius: 25px
        }
        footer{
            text-align: right;
            margin-top: 100px;
            height: 30px;
            position: absolute;
            bottom: 80px;
            right: 400px;
        }
        textarea {
            outline: none;
            padding: 2px 4px;
        }
    </style>
</head>
<body>
    <div id="wrap">    
        <header>
            <h1>Blue BLOG</h1>
            <ul>
                <li><button id="home_btn">홈</button></li>
                <li><button id="write">글쓰기</button></li>
            </ul>
        </header>
        <section>
            <div id=content_list>
                <ul>
                    <?php
                    $lists = scandir('data/');
                    for($i=0; $i<count($lists); $i++) {
                        $title = $lists[$i];
                        if($lists[$i] != "." && $lists[$i] != "..") {
                            echo "<li><a href='index.php?title=${title}'>${title}<a></li>";
                        }
                    }
                    ?>
                </ul>
            </div>
            <div id=content>
                <h2>
                    <?php
                        echo $_GET['title'];
                    ?>
                </h2>
                <p>
                    <?php
                        echo file_get_contents("data/".$_GET['title']);
                    ?>
                </p>
            </div>
            <div id=main>
                <form action="write_process.php" method="post">
                    <textarea name="text_title" id="text_title" cols="30" rows="10" placeholder="제목을 입력해주세요"></textarea>
                    <textarea name="text_area" id="text_area" cols="30" rows="10" placeholder="내용을 입력해주세요"></textarea>
                    <div id="btn">
                    <button type="submit">등록</button>
                    <button type="reset">취소</button>
                    </div>
                </form>
            </div>
            <div id=home>
                <h2>홈 화면입니다 ~</h2>
                <p>글 쓰시려면 위에 글쓰기 버튼 눌러주세용 ~</p>
            </div>
        </section>
        <footer>
            <p>즐거운 PHP</p>
        </footer>
    </div>
    <script>
        let main = document.querySelector('#main');
        let home = document.querySelector('#home');
        let btn = document.querySelector('#home_btn');
        let btn2 = document.querySelector('#write');
        
        btn2.addEventListener('click', function(){
            main.style.display = 'block';
            home.style.display = 'none';
        })
        btn.addEventListener('click', function(){
            main.style.display = 'none';
            home.style.display = 'block';
        })
    </script>
</body>
</html>

 

write_process.php

<?php
    // file_put_contents(파일명, 내용);
    // http://localhost/php/file/write_process.php?title=추가할제목&desc=내용입니다.
    $title = $_POST['text_title'];   // $_GET
    $description = $_POST['text_area'];  // $_GET
   
    file_put_contents('./data/'.$title, $description);
    echo "파일이 추가되었습니다.";

    // 리다이렉션
    header('Location:index.php');
?>