sub_title
 PHP
제   목 PHP - 파일다운로드 함수
작성자 오렌지 등록일 2009-03-07 01:19:34 조회수 12,941
PHP - 파일다운로드 함수


php용 파일다운로드 함수로 주석을 보시면 아시겠지만 해외 사이트에서 가져온 소스입니다. www.swish-db.com의 파일다운로드 처리를 묻는 질문에 대한 답변으로 올려진 소스인데 재밌는 것은 답변하신 분도 첫번째 출처를 모른다는 것입니다. 하지만 분명히 기억하는 것은 copyright free 랍니다. 사용법은 다운로드할 파일명을 받아서 함수 인자로 넘겨주시면 됩니다. 테스트 해 보니까 잘 동작합니다.


<?php
//출처-http://www.swish-db.com

dl_file("file.zip"); // or whatever the file name is.
function dl_file($file){
//First, see if the file exists
if (!is_file($file)) { die("<b>404 File not found!</b>"); }

//Gather relevent info about file
$len = filesize($file);
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
//This will set the Content-Type to the appropriate setting for the file
switch( $file_extension ) {
   case "pdf": $ctype="application/pdf"; break;
   case "exe": $ctype="application/octet-stream"; break;
   case "zip": $ctype="application/zip"; break;
   case "doc": $ctype="application/msword"; break;
   case "xls": $ctype="application/vnd.ms-excel"; break;
   case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
   case "gif": $ctype="image/gif"; break;
   case "png": $ctype="image/png"; break;
   case "jpeg":
   case "jpg": $ctype="image/jpg"; break;
   case "mp3": $ctype="audio/mpeg"; break;
   case "wav": $ctype="audio/x-wav"; break;
   case "mpeg":
   case "mpg":
   case "mpe": $ctype="video/mpeg"; break;
   case "mov": $ctype="video/quicktime"; break;
   case "avi": $ctype="video/x-msvideo"; break;

//The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
   case "php":
   case "htm":
   case "html":
   case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;

default: $ctype="application/force-download";
}
//Begin writing headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");

//Use the switch-generated Content-Type
header("Content-Type: $ctype");

//Force the download
$header="Content-Disposition: attachment; filename=".$filename.";";
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len);
readfile($file);
exit;
}
?>



 
0
    
 
0
        list
 
※ 짧은 댓글일수록 예의를 갖추어 작성해 주시기 바랍니다.
line
reply cancel
 
번호 제목 글쓴이 추천 조회 날짜
55  PHP - 파일다운로드 함수   member 오렌지 0 / 0 12941 2009-03-07
54  fsockopen 함수를 이용해서 웹사이트에 데이터 전송하기   member 높이뜬새 2 / 0 11593 2009-03-06
53  PHP - 웹사이트의 유효성 체크   member 오렌지 0 / 0 7963 2009-02-28
52  PHP 현재 페이지의 도메인명이나 url등의 정보 알아오기   member 높이뜬새 6 / 0 108283 2009-02-27
51  PHP 파일 한줄 읽기(fgets함수)   admin 웹마당넷 0 / 0 32677 2009-02-24
50  PHP 파일 내용 읽기(fread함수)   admin 웹마당넷 3 / 0 27189 2009-02-15
49  PHP 파일 시스템(File System) 다루기 #1   admin 웹마당넷 0 / 0 21355 2009-02-14
48  PHP 썸네일(Thumbnail) 이미지 만들기 함수 첨부파일   member 티란투라 0 / 0 10646 2009-02-08
47  PHP - xml 읽어들이기(read) 클래스   member 오렌지 0 / 0 9398 2009-02-08
46  PHP와 자바스크립트를 이용한 드롭다운 메뉴 첨부파일   member 오렌지 0 / 0 11843 2009-02-07
45  PHP로 MySQL 데이터베이스 만들기(생성하기)   admin 웹마당넷 1 / 0 10407 2009-01-24
44  PHP MySQL 명령어 실행 함수 mysql_query   admin 웹마당넷 1 / 0 47227 2009-01-21
43  PHP 에러 처리 함수 die   admin 웹마당넷 4 / 0 41735 2009-01-20
42  PHP no-cache 설정(뒤로가기 막기)   admin 웹마당넷 1 / 0 31224 2009-01-17
41  PHP 폼(form)값의 유효성 체크   admin 운영자 0 / 0 9769 2009-01-13
40  PHP - SELECT로 반환된 레코드 갯수와 컬럼의 갯수를 알려주는 함수   admin 웹마당넷 0 / 0 18910 2009-01-11
39  PHP 버전 확인하기   admin 웹마당넷 0 / 0 13007 2009-01-06
38  PHP 한글 문자열 자르기   admin 웹마당넷 0 / 0 11834 2008-12-27
37  PHP 오늘날짜를 기준으로 이전,이후 날짜구하기   admin 웹마당넷 0 / 1 15207 2008-12-25
36  PHP 이름이 같은 폼태그(Form Tag)의 값(value) 처리하기   admin 웹마당넷 0 / 0 33195 2008-12-06
35  PHP split 함수를 이용한 문자열 분리   admin 웹마당넷 1 / 0 52818 2008-12-06
34  PHP explode 함수를 이용한 문자열 분리   admin 웹마당넷 0 / 0 11565 2008-12-01
33  PHP 달력(Calendar) 입니다. 이미지  [1] member 티란투라 0 / 0 11330 2008-11-16
32  플래쉬 무비클립을 캡쳐하는 PHP 소스 입니다. 첨부파일   member 사라마음 0 / 0 7103 2008-11-14
31  PHP에서 세션(session) 사용하기   member 웹스톤 0 / 0 14094 2008-08-31
write
button [1] [3] [4] button