메뉴 건너뛰기

프로그램언어

조회 수 28547 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

PHP를 이용한 간단한 파일 업로드, 다운로드를 가능하게 하는 소스다. 요즘은 함수나 클래스로 구현하는 경우가 많지만, 간단하게 사용할 수 있도록 별다른 기능을 넣지는 않았다.

이 소스를 기본으로 다중파일 업로드와 업로드 된 파일을 다운로드 받을 시, 파일 확장자를 체크하여 이미지 파일이면 다운로드 받지 않고 브라우저에서 직접 보이도록 하는 기능은 직접 구현하기 바란다.

Up_Load_Form.html (파일 업로드 화면)

<form name="form1" method="post" enctype="multipart/form-data" action="File_Upload.php">
<table width="600" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
   <td colspan="2" bgcolor="#FFFFFF">PHP를 이용한 파일업로드 기능의 구현</td>
</tr>
<tr>
   <td width="150" align="center" bgcolor="#FFFFFF">업로드할 파일</td>
   <td width="464" bgcolor="#FFFFFF"><input type="file" name="myFile" size="60" /></td>
</tr>
<tr>
   <td colspan="2" bgcolor="#FFFFFF"><input type="submit" value="파일 업로드" />
   <input type="reset" value="취소" /></td>
</tr>
</table>
</form>

File_Upload.php (파일 업로드)

<? 
//업로드한 파일을 저장할 디렉토리 
$save_dir = "files/"; 
  
//파일이 HTTP POST 방식을 통해 정상적으로 업로드되었는지 확인한다. 
if(is_uploaded_file($_FILES["myFile"]["tmp_name"])) 
{ 
   echo "업로드한 파일명 : ".$_FILES["myFile"]["name"] ."<br />"; 
   echo "업로드한 파일의 크기 : ".$_FILES["myFile"]["size"] ."<br />"; 
   echo "업로드한 파일의 MIME Type : ".$_FILES["myFile"]["type"] ."<br />"; 
   echo "임시 디렉토리에 저장된 파일명 : ".$_FILES["myFile"]["tmp_name"]."<br />"; 
  
   //파일을 저장할 디렉토리 및 파일명 
   $dest = $save_dir . $_FILES["myFile"]["name"]; 
  
   //파일을 지정한 디렉토리에 저장 
   if(!move_uploaded_file($_FILES["myFile"]["tmp_name"], $dest)) 
   { 
      die("파일을 지정한 디렉토리에 저장하는데 실패했습니다."); 
   } 
} 
?> 

필자의 경우 파일 중복을 피하기 위해서
$fn = $_FILES["myFile"]["name"];
$fn = mktime()."^".$fn;
DB 삽입시 파일명을 (시간 + ^+ 파일명) 형태로 가공해서 넣었다. 나중에 다운로드를 구현할 때 "^" 기준으로 잘라내면 뒷부분이 원본 파일 이름이 된다.

다운로드 링크

<a href="down('파일명')">다운로드</a>
<script>
function dwon(fileName)
{
   url = "Down_Load.php"+fileName;
   location.href = url;
}
</script>


Down_Load.php (다운로드 받는 페이지 구현)

<? 
$fileName = $_REQUEST[fileName]; 
$DownloadPath = "./files/".$fileName; // 파일 경로 
$fileTmp = strstr($fileName, '^'); // 파일명 임시저장(앞의 '^'를 제거 
$DownFile = substr($fileTmp, 2); 
  
Header("Content-Type: file/unknown"); 
Header("Content-Disposition: attachment; filename=". $DownFile; 
Header("Content-Length: ".filesize("$DownloadPath")); 
header("Content-Transfer-Encoding: binary "); 
Header("Pragma: no-cache"); 
Header("Expires: 0"); 
flush(); 
  
if ($fp = fopen("$downloadPath", "r")) 
{ 
   print fread($fp, filesize("$DownloadPath")); 
} 
fclose($fp); 
?>




  1. No Image 07Mar
    by
    2017/03/07 Views 40187 

    리다이렉션(페이지 이동)의 3가지 방법, location.href

  2. No Image 19Feb
    by
    2017/02/19 Views 35888 

    [PHP] 게시판 글쓰기와 이미지 파일 DB 저장 및 불러오기 예제

  3. No Image 21Sep
    by
    2016/09/21 Views 33237 

    $_SERVER 환경변수

  4. No Image 27Feb
    by
    2014/02/27 Views 32777 

    PHP에서 PDF파일 생성하기

  5. No Image 27Feb
    by
    2014/02/27 Views 31635 

    PHP에서 자바스크립트 값 가져오기

  6. No Image 27Feb
    by
    2014/02/27 Views 30504 

    Class를 이용한 DB Connection 소스 (Oracle, MyS

  7. No Image 12Apr
    by
    2014/04/12 Views 30443 

    htmlentities <-> html_entity_decode (엔티티

  8. No Image 27Feb
    by
    2014/02/27 Views 30280 

    무조건 알아야 할 PHP 속도 테스트 14 가지

  9. No Image 27Feb
    by
    2014/02/27 Views 30257 

    PHP로 Excel 파일 만들기...

  10. No Image 27Feb
    by
    2014/02/27 Views 29813 

    Text를 GD 이미지로 뿌리기

  11. No Image 07Mar
    by
    2017/03/07 Views 29787 

    php 엑셀 다운로드 구현

  12. No Image 27Feb
    by
    2014/02/27 Views 29446 

    글내용 이미지 리사이징

  13. No Image 27Feb
    by
    2014/02/27 Views 29211 

    쿠키변수받기

  14. No Image 27Feb
    by
    2014/02/27 Views 29201 

    Record Drag/Drop Position

  15. No Image 06Mar
    by 조쉬
    2017/03/06 Views 28547 

    간단한 PHP 파일 업로드, 다운로드 구현

  16. No Image 27Feb
    by
    2014/02/27 Views 28332 

    DB상의 많은 파일을 한꺼번에 다운받기

  17. No Image 27Feb
    by
    2014/02/27 Views 26945 

    GD를 이용한 스팸성 게시물 차단을 위한 보안 단어 입력 예제

  18. No Image 27Feb
    by
    2014/02/27 Views 26731 

    주간날짜 뽑아오기

  19. No Image 14Apr
    by
    2015/04/14 Views 26622 

    전화번호에 하이픈(-) 넣기

  20. No Image 14Apr
    by
    2015/04/14 Views 26575 

    PHP 변수전달 GET, POST

Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved