메뉴 건너뛰기

프로그램언어

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

단축키

Prev이전 문서

Next다음 문서

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

단축키

Prev이전 문서

Next다음 문서

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

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


<? 
/*************************************************************** 
프로그램명 : filedownload_zip.php 
설명 : 파일 여러개 zip 파일로 묶어서 받기 
작성일자 : 2005-10-06 오후 1:58 
작성자 : 윤영식 
***************************************************************/ 

// 에러 메시지 뿌리고 history.back() 
function error($msg) { 
echo "<script language="javascript">rn"; 
echo " alert("".$msg."");rn"; 
echo " history.back();rn"; 
echo "</script>"; 
exit(); 
} 

// 디렉토리 통째로 삭제하기.... 
function rm($fileglob) { 
if (is_string($fileglob)) { 
if (is_file($fileglob)) { 
return unlink($fileglob); 
} else if (is_dir($fileglob)) { 
$ok = rm("$fileglob/*"); 
if (! $ok) { 
return false; 
} 
return rmdir($fileglob); 
} else { 
$matching = glob($fileglob); 
if ($matching === false) { 
trigger_error(sprintf('No files match supplied glob %s', $fileglob), E_USER_WARNING); 
return false; 
} 
$rcs = array_map('rm', $matching); 
if (in_array(false, $rcs)) { 
return false; 
} 
} 
} else if (is_array($fileglob)) { 
$rcs = array_map('rm', $fileglob); 
if (in_array(false, $rcs)) { 
return false; 
} 
} else { 
trigger_error('Param #1 must be filename or glob pattern, or array of filenames or glob patterns', E_USER_ERROR); 
return false; 
} 
return true; 
} 

// 업로드 파일 디렉토리 
$up_path = "../upload"; 
$zip_dir = "../upload/tmp_zip"; 

// tmp 디렉토리 만들기 위해서 file_dir 만들기 
$tmp = microtime(); 
$tmp2 = explode(" ",$tmp); 
$file_dir = $tmp2[1].sprintf("%03d",(int)($tmp2[0]*1000)); 
$tmp=null; 
$tmp2=null; 

// tmp 디렉토리 
$tmp_dir = $zip_dir."/".$file_dir; 

// tmp 디렉토리 생성 
if(!mkdir ($tmp_dir, 0700)) { 
error("파일 생성시 에러가 발생했습니다."); 
exit; 
} 

$sql = "SELECT id, filename FROM table"; 
$result = mysql_query($sql); 
$i=0; 
while($row = mysql_fetch_assoc($result)) { 
$org_name = "/".$row[file_name]; 
$new_name = "/".$id."_".$row[file_name]; 

// tmp 디렉토리로 COPY 
@copy($up_path.$org_name,$tmp_dir.$new_name); 
$i++; 
} 
mysql_free_result($result); 
mysql_close(); 

if (!$i) { 
rm($tmp_dir); 
error("선택된 파일이 없습니다."); 
exit; 
} 

// zip 으로 묶기 
// zip 경로는 서버에서 which zip 을 통해서 알아낸다. 
@exec("/usr/bin/zip ".$tmp_dir."/".$file_dir.".zip ".$tmp_dir."/*"); 

// 다운로드 받기 
if( $fp = @fopen( $tmp_dir."/".$file_dir.".zip","r")) { 
Header("Content-type: file/unknown"); 
Header("Content-Disposition: attachment; filename=".$file_dir.".zip"); 
Header("Content-Description: PHP3 Generated Data"); 

while ($data=fread($fp, filesize( $tmp_dir."/".$file_dir.".zip"))){ 
print($data); 
} 
} else { 
error("서버에 자료파일이 없습니다."); 
} 

// 임시로 만들어진 모든 데이터 지우기, 단, 사용자가 취소를 누르면 서버상에 임시데이터가 지워지지 않으니 cron 을 통해 하루에 한번씩 지워주면 된다. 
rm($tmp_dir); 
exit; 
?> 


List of Articles
번호 제목 날짜 조회 수
160 PHP의 유동변수!? - $a1 ~ $a2 같은 형식의 변수를 반복문 돌릴때... 2017.03.06 16610
159 PHP웹 보안 취약점 TOP5(웹해킹) 2023.01.12 292
158 PHP와 HTML과 자바스크립트의 관계 2021.03.26 311
157 PHP에서의 대칭 암호화/복호화 ― 간단한 예제에서 DB 입/출력까지 2018.09.14 3548
156 php에서 체크박스 선택한 것 보여주기 file 2019.01.08 1805
155 PHP에서 조건문 처리 2015.04.14 22038
154 PHP에서 자바스크립트 값 가져오기 2014.02.27 31635
153 PHP에서 자료, 데이터의 타입을 확인하는 방법, gettype() 2018.08.29 2447
152 PHP에서 암호화 encrypt 복호화 decrypt 해서 값을 넘기기 2018.02.09 10626
151 PHP에서 모든 세션 정보를 화면에 출력하는 방법 2018.08.29 2693
150 PHP에서 데이터를 엑셀(Excel)로 저장 2017.02.19 18428
149 PHP에서 UTF와 EUC-KR 변환 2019.02.19 1553
148 PHP에서 PDF파일 생성하기 2014.02.27 32777
147 PHP에서 Excel 파일을 만들 수 있는 PHPExcel file 2017.03.06 17112
146 PHP에서 CSV 파일 export file 2016.04.22 22333
145 PHP로 엑셀 자료 MySQL에 넣기 2017.03.06 17875
144 PHP로 Excel 파일 만들기... 2014.02.27 30257
143 php로 db 컨트롤 1 2017.03.06 15769
142 phpMyAdmin WebMysql 에 CSV 엑셀 파일 업로드 입력하기 ( Excel / Upload / data / 데이터 / 데이타 ) file 2021.03.25 754
141 phpexcel을 이용한 PHP로 엑셀파일 읽기와 생성 file 2017.03.06 22787
Board Pagination Prev 1 ... 5 6 7 8 9 10 11 12 13 14 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved