본문 바로가기

프론트앤드 수업

[php] 파일 수정,삭제,추가를 이용해 게시판 만들기

728x90

기본화면

홈 - index.php 로 링크

글쓰기 - write.php 로 링크

    <?php include_once 'include/header.php'; ?>
        <div id="contents">
            <h2>도서리스트</h2>
            <ul>
              <?php   createList();      ?>
            </ul>
            <h2>
                <?php  printTitle(); ?>
            </h2>
            <div id="bookinfo">
                <?php 
                    printDesc(); 
                ?>
                <?php if(isset($_GET['id'])){ ?>
                    <ul>
                    <li><a href="edit.php?id=<?=$_GET['id']?>">글수정</a></li>
                    <li>
                        <form action="process/delete_process.php" method="post">
                            <input type="hidden" name="id" value="<?=$_GET['id']?>">
                            <input type="submit" id="id" value="글삭제">
                        </form>    
                    </li>
                    </ul> 
                    <?php     } ?>

            </div>
        </div>
    <?php include_once 'include/footer.php'; ?>

include를 이용해 컨텐츠만 수정할 수 있도록 하였다.

글쓰기 화면이다.

wirte.php로 이동하고 inclue로 나눠놓은 헤더와 푸터만 포함시키고 컨텐츠만 작성해서 사용

        <header>
            <h1>
                <a href="index.php">Blog</a>
            </h1>
            <ul>
                <li><a href="index.php">홈</a></li>
                <li><a href="create.php">글쓰기</a></li>
            </ul>
        </header>

header에서 글쓰기 선택시 create.php로 이동한다.

<?php include_once 'include/header.php'; ?>
    <div id="contents">
    <h2>도서 등록하기</h2>
    <form action="process/write_process.php" method="post">
    <table>
        <tr>
            <td>글제목</td>
            <td><input type="text" name="id"></td>
        </tr>
        <tr>
            <td>글내용</td>
            <td><textarea name="description" id="description" cols="30" rows="10"></textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <button type="submit">글적기</button>
                <button type="reset">취소</button>
            </td>
        </tr>
    </table>
</form>
</div>


<?php include_once 'include/footer.php'; ?>

create.php는 post로 값을 받아 그대로 write_process.php 로 전송해준다.

<?php
    // var_dump($_POST);
    $id = $_POST['id'];
    $desc = $_POST['description'];

    file_put_contents('../data/'.$id,$desc);

?>

<script>
        location.replace('../index.php')
    </script>

post로 받은 값의 제목과 내용을 이용해 data폴더안에 파일을 생성해준다.

글삭제를 누를시 즉시 삭제되고 index.php로 이동

<?php  
//파일 삭제하기
unlink('../data/'.$_POST['id']);

?>
<script>
        location.replace('../index.php')
    </script>

unlink를 이용해 받아온 파일의 제목을 이용해 삭제한다.

글수정선택시 edit.php로 이동한다.

<?php include_once 'include/header.php'; ?>
<div id="contents">
<h2>도서 수정하기</h2>
    <form action="process/edit_process.php" method="post">
    <table>
        <tr>
            <td>글제목</td>
            <td><input type="hidden" name="old_id" required value="<?=$_GET['id']?>"></td>
            <td><input type="text" name="id" required value="<?=$_GET['id']?>"></td>
        </tr>
        <tr>
            <td>글내용</td>
            <td><textarea name="description" id="description" cols="30" rows="10" required  >
                <?php 
                    echo file_get_contents('data/'.$_GET['id']);
                ?>
            </textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <button type="submit">글수정</button>
                <button type="reset">취소</button>
            </td>
        </tr>
    </table>
</form>

</div>
<?php include_once 'include/footer.php'; ?>

edit는 기본값이 들어가있어야하기에

     <ul>
                    <li><a href="edit.php?id=<?=$_GET['id']?>">글수정</a></li>
                    <li>
                        <form action="process/delete_process.php" method="post">
                            <input type="hidden" name="id" value="<?=$_GET['id']?>">
                            <input type="submit" id="id" value="글삭제">
                        </form>    
                    </li>
                    </ul> 

글수정에서 기본값을 받아서 old_id로 하나더 전송해준다.

..화면ㅇ ㅣ왜이런지 도저히 모르겠어서 포기했다...

td가 hidden으로 넣었는데 자꾸 한자리 차지한다 ㅠㅠ...

<?php include_once 'include/header.php'; ?>
<div id="contents">
<h2>도서 수정하기</h2>
    <form action="process/edit_process.php" method="post">
    <table>
        <tr>
            <td>글제목</td>
            <td><input type="hidden" name="old_id" required value="<?=$_GET['id']?>"></td>
            <td><input type="text" name="id" required value="<?=$_GET['id']?>"></td>
        </tr>
        <tr>
            <td>글내용</td>
            <td><textarea name="description" id="description" cols="30" rows="10" required  >
                <?php 
                    echo file_get_contents('data/'.$_GET['id']);
                ?>
            </textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <button type="submit">글수정</button>
                <button type="reset">취소</button>
            </td>
        </tr>
    </table>
</form>

</div>
<?php include_once 'include/footer.php'; ?>

기본화면에 value값을 넣어준다.

_1과 수정을 지우고 수정시 수정이 완료된다.

<?php
  // 파일이름 변경하기 rename(파일이름. 변경할이름)
  // 내용 변경 file_put_contents(파일이름, 내용)
    rename('../data/'.$_POST['old_id'],'../data/'.$_POST['id']);
    file_put_contents('../data/'.$_POST['id'],$_POST['description']);

?>
<script>
        location.replace('../index.php')
    </script>

rname을 이용해 파일제목변경 및 file_put_contents 를 이용해 파일의 내용도 변경하여준다.

728x90
댓글