PHP
파일 다운로드
do_it0904
2018. 11. 20. 18:21
*** fileUpload.html
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 | <?php //DB 연결 require_once("/usr/local/apache/htdocs/dbconfig.php"); $sql = 'SELECT * FROM file'; $result = mysql_query($sql, $db); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <!-- 합쳐지고 최소화된 최신 CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"> <!-- 부가적인 테마 --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css"> <script> function down(seq){ location.href = "downLoad.php?file_seq="+seq; } </script> <title>Document</title> <body> <div> <form enctype='multipart/form-data' action='upload.php' method='POST'> <input type="hidden" vlaue="30000" name="MAX_FILE_SIZE"> <input type='file' name='image'> <input type="submit" value="보내기"> </form> </div> <div style="width:50%; padding-top:30px;"> <table class="table table-hover"> <thead> <tr> <td>SEQ</td> <td>파일명</td> <td>파일 저장이름</td> <td>등록일자</td> <td></td> </tr> </thead> <tbody> <?php while($row = mysql_fetch_assoc($result)){ echo "<tr> <td>".$row['file_seq']."</td> <td>".$row['name_org']."</td> <td>".$row['name_save']."</td> <td>".$row['reg_time']."</td> <td><input class='btn btn-primary btn-sm' type='button' value='다운' onclick=down($row[file_seq])></td> </tr>"; } ?> </tbody> </table> </div> </body> </html> | cs |
DB에 담긴 업로드 파일 정보들을 불러 조회하였다. 정보로는 파일명(실제 파일이름), 파일 저장 시 이름, 등록일자로 구성되어있다.
***downLoad.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 | <?php //DB 연결 require_once("/usr/local/apache/htdocs/dbconfig.php"); $seq = $_GET['file_seq']; $sql = "SELECT * FROM file WHERE file_seq = ".$seq; $result = mysql_query($sql, $db); $row = mysql_fetch_assoc($result); $name_org = $row['name_org']; $name_save = $row['name_save']; $fileDir = "/usr/local/apache/htdocs/uploads"; $fullPath = $fileDir.$name_save; $size = filesize($fileDir); echo "name_org : ".$name_org."<br/>\n"; echo "name_save : ".$name_save."<br/>\n"; echo "fileDir : ".$fileDir."<br/>\n"; echo "fullPath : ".$fullPath."<br/>\n"; echo "size : ".$size."<br/>\n"; header("Content-Type: application/octet-stream"); header("Content-size: ".$size); header("Content-Disposition: attachment; filename=".$name_org); header("Content-Transfer-Encoding: binary"); $fh = fopen($fullPath, "r"); fpassthru($fh); ?> | cs |
파일 정보를 가지고 다운로드 처리하는 공간이다. 이렇게하니 다음과 같은 오류가 발생한다.
찾아보니
header를 사용하기전 디버깅용으로 출력문을 작성하면 발생하는 오류였다. 지워주니 정상작동된다.
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 | <?php //DB 연결 require_once("/usr/local/apache/htdocs/dbconfig.php"); $seq = $_GET['file_seq']; $sql = "SELECT * FROM file WHERE file_seq = ".$seq; $result = mysql_query($sql, $db); $row = mysql_fetch_assoc($result); $name_org = $row['name_org']; $name_save = $row['name_save']; $fileDir = "/usr/local/apache/htdocs/uploads"; $fullPath = $fileDir.$name_save; $size = filesize($fileDir); header("Content-Type: application/octet-stream"); header("Content-Length: ".$size); header("Content-Disposition: attachment; filename=\"$name_org\""); header("Content-Transfer-Encoding: binary"); $fh = fopen($fullPath, "r"); fpassthru($fh); echo "name_org : ".$name_org."<br/>\n"; echo "name_save : ".$name_save."<br/>\n"; echo "fileDir : ".$fileDir."<br/>\n"; echo "fullPath : ".$fullPath."<br/>\n"; echo "size : ".$size."<br/>\n"; ?> | cs |
***디렉토리와 DB내 파일 삭제
(onclick을 통해 파일명을 보내줬으면 SELECT문 없이 바로 되었을텐데, Uncaught ReferenceError: Rev is not defined 라는 오류 발생)
(삭제 시, 파일의 원래 이름과 경로만 주어지면 가능)
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 | <?php require_once("/usr/local/apache/htdocs/dbconfig.php"); $file_seq = $_GET['seq']; $save_dir = "uploads/"; //이름조회 $sql = 'SELECT * FROM file WHERE file_seq = '.$file_seq.''; $result = mysql_query($sql, $db); $row = mysql_fetch_assoc($result); $name_org = $row['name_org']; //DB 삭제 $sql = 'DELETE FROM file WHERE file_seq = '.$file_seq.''; $result = mysql_query($sql, $db) or die(mysql_error()); //디렉토리 삭제 unlink($save_dir.$name_org); echo "<meta http-equiv='refresh' content='0; url=fileUpload.html'>"; ?> | cs |