웹(php)/웹(file)_php

[Project 2]PHP로 FILE 업로드_index.php(파일 리스트)

byeol2ing 2016. 11. 8. 19:31
반응형

PHP로 FILE 업로드  

필요한 파일 

- files( 파일이 저장될 파일)

- index.php(파일 리스트)

- delete.php(삭제)

- download.php(파일 다운)

- table.php(게시판 작성 양식)

- write.php(파일이 db에 저장되는 것을 처리)

- view.php(파일의 내용을 볼수 있는 곳)


index.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<html>
<head>
    <meta charset="utf-8"/>
    <title>file list</title>
</head>
<body>
<?php
$host = 'localhost';
$user = 'root';
$pw = '111111';
$dbName = 'opentutorials';
$mysqli = new mysqli($host, $user, $pw, $dbName);
 if(mysqli_connect_errno())
        {
            echo "DB connect error";
        }
        
        $sql = "select * from a";
        $res = $mysqli->query($sql);
        $num_result = $res->num_rows;
    ?>
    
    
    
    <table border='1' align="center">
        <thead>
            <tr>
                <th width="50">NUM</th>
                <th width="250">FILE</th>
                <th width="200">TIME</th>
                <th width="50">id</th>
                <th width="70">subject</th>
                <th width="250">memo</th>
                <th width="70">DOWN</th>
                <th width="50">DEL</th>
                
 
            </tr>
        </thead>
        <tbody>
            <?php
                for($i=0; $i<$num_result; $i++)
                {
                    $row = $res->fetch_assoc();
                    echo "<tr>";
                    echo "<td align='center'>".$row['num']."</td>";
                    echo "<td align='left'>
                <a href='./download.php?num=".$row['num']."'>".$row['name']."</a></td>";
                    echo "<td align='center'>".$row['time']."</td>";
                    echo "<td align='center'>".$row['id']."</td>";
                    echo "<td align='center'>
                <a href='./view.php?num=".$row['num']."'>".$row['subject']."</a></td>";    
                    echo "<td align='center'>".$row['memo']."</td>";
                    echo "<td align='center'>".$row['down']."</td>";    
                    echo "<td align='center'>
                <a href='./delete.php?num=".$row['num']."'>DEL</a></td>";                    
                    echo "</tr>";
                }
              mysqli_close($mysqli);
 
              
            ?>
            <input type = "button" name = "table" value ="글쓰기" onclick = "location.href='table.php'";>
        </tbody>
cs

이것인데요........ㅠㅠㅠ 저게뭐람... 

<?php
$host = 'localhost';
$user = 'root';
$pw = '111111';
$dbName = 'opentutorials';
$mysqli = new mysqli($host, $user, $pw, $dbName);
 if(mysqli_connect_errno())
        {
            echo "DB connect error";
        }
        

sql 연동 부분입니다.


$sql = "select * from a";
        $res = $mysqli->query($sql);
        $num_result = $res->num_rows;


$mysqli->query($sql); 이부분은 쿼리문을 이용해 sql 테이블 a 에 저장된 모든 데이터를 선택하여 쿼리 결과를 생성시킵니다. 

$res->num_rows; num_result에는 db결과가 몇개인지에 대한 정보가 들어갑니다.



<table border='1' align="center">
        <thead>
            <tr>
                <th width="50">NUM</th>
                <th width="250">FILE</th>
                <th width="200">TIME</th>
                <th width="50">id</th>
                <th width="70">subject</th>
                <th width="250">memo</th>
                <th width="70">DOWN</th>
                <th width="50">DEL</th>
                
 
            </tr>
        </thead>
        <tbody>


표의 굵기를 1(border='1')로 하여 가운데 정렬(align="center")하여 표를 만듭니다.

<th>내용</th>는 tale head의 약자로, 표의 제목을 쓰는역할입니다. (기본값 : 굵은글씨, 중앙정렬)

<tr>내용</tr>은 table row의 약자로, 가로줄을 만듭니다. (기본값 : 보통글씨, 왼쪽정렬)

<td>내용</td>는 table data의 약자로, 셀을 만드는 역할입니다. (기본값 : 보통글씨, 왼쪽정렬)


<tr>

<th>이름</th>

<th>나이</th>

</tr>

<tr>

<td>김영수</td>

<td>24</td>

</tr>


일경우


이름

나이

김영수

24

이렇게 된다는 말이죠 !



<?php
                for($i=0; $i<$num_result; $i++)
                {
                    $row = $res->fetch_assoc();
                    echo "<tr>";
                    echo "<td align='center'>".$row['num']."</td>";
                    echo "<td align='left'>
                <a href='./download.php?num=".$row['num']."'>".$row['name']."</a></td>";
                    echo "<td align='center'>".$row['time']."</td>";
                    echo "<td align='center'>".$row['id']."</td>";
                    echo "<td align='center'>
                <a href='./view.php?num=".$row['num']."'>".$row['subject']."</a></td>";    
                    echo "<td align='center'>".$row['memo']."</td>";
                    echo "<td align='center'>".$row['down']."</td>";    
                    echo "<td align='center'>
                <a href='./delete.php?num=".$row['num']."'>DEL</a></td>";                    
                    echo "</tr>";
                }
              mysqli_close($mysqli);
 
              
            ?>


for문을 이용해 총 num_result의 갯수($res->num_rows; num_result에는 db결과가 몇개인지에 대한 정보가 들어갑니다.) 만큼 테이블을 동적으로 생성하게 됩니다.

 $row = $res->fetch_assoc();

각 반복문이 돌아갈때마다 row변수에 select로 뽑아온 데이터가 배열로 저장됩니다.


echo "<td align='center'>".$row['num']."</td>"; 

row변수의 num에 저장되어있는 것이 입력 되겠죠


echo "<td align='left'>

<a href='./download.php?num=".$row['num']."'>".$row['name']."</a></td>";

파일의 name에 하이퍼 링크를 걸어(클릭시 download.php로 이동시키도록)줍니다.


mysqli_close($mysqli);

그리고 sql을 종료시켜줍니다.

결과입니다.




반응형