메뉴 건너뛰기

프로그램언어

조회 수 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); 
?>




List of Articles
번호 제목 날짜 조회 수
340 $_FILES 2016.12.23 23847
339 $_SERVER 함수 2016.12.23 23943
338 $_SERVER 환경변수 2016.09.21 33237
337 $_SERVER변수 2014.02.27 24443
336 13자리 timestamp 생성하기 file 2020.09.28 649
335 addslashes — 문자열을 슬래시로 인용 2016.12.23 23083
334 addslashes 함수의 필요성 2015.04.14 24250
333 ajax refresh 시키기(자동리플래쉬) with php file 2017.03.06 23185
332 Ajax로 구연한 실시간 서버시간출력 file 2017.03.06 21031
331 AJAX로 해당 페이지에서 COOKIE 사용하기 2021.03.26 359
330 AJAX를 활용하여 JSON 댓글 처리하기 (PHP) 2018.07.04 8454
329 array (배열) 2015.04.14 24904
328 array_key_exists 배열에서 key가 존재하는지 확인 2016.12.23 22206
327 array_push 배열 끝에 하나 이상의 요소를 추가 2016.12.23 21604
326 array_slice 배열의 일부를 추출 2016.12.23 20775
325 base64 인코딩/디코딩 함수의 특징 file 2018.02.09 13078
324 call_user_func 사용자가 정의한 함수를 호출하여 실행고자 할 때 사용 2016.12.23 21302
323 class_exists 클래스가 정의되었는지 확인 2016.12.23 19881
322 Class를 이용한 DB Connection 소스 (Oracle, MyS 2014.02.27 30503
321 CodeIgniter - DB오류체크, 디버깅 여부 설정 2021.03.29 494
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved