메뉴 건너뛰기

프로그램언어

조회 수 28338 추천 수 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; 
?> 


  1. PHP 변수전달 GET, POST

    Date2015.04.14 Views26596
    Read More
  2. 전화번호에 하이픈(-) 넣기

    Date2015.04.14 Views26638
    Read More
  3. 주간날짜 뽑아오기

    Date2014.02.27 Views26738
    Read More
  4. GD를 이용한 스팸성 게시물 차단을 위한 보안 단어 입력 예제

    Date2014.02.27 Views26951
    Read More
  5. DB상의 많은 파일을 한꺼번에 다운받기

    Date2014.02.27 Views28338
    Read More
  6. 간단한 PHP 파일 업로드, 다운로드 구현

    Date2017.03.06 Views28559
    Read More
  7. Record Drag/Drop Position

    Date2014.02.27 Views29215
    Read More
  8. 쿠키변수받기

    Date2014.02.27 Views29234
    Read More
  9. 글내용 이미지 리사이징

    Date2014.02.27 Views29451
    Read More
  10. php 엑셀 다운로드 구현

    Date2017.03.07 Views29802
    Read More
  11. Text를 GD 이미지로 뿌리기

    Date2014.02.27 Views29821
    Read More
  12. PHP로 Excel 파일 만들기...

    Date2014.02.27 Views30262
    Read More
  13. 무조건 알아야 할 PHP 속도 테스트 14 가지

    Date2014.02.27 Views30285
    Read More
  14. Class를 이용한 DB Connection 소스 (Oracle, MyS

    Date2014.02.27 Views30508
    Read More
  15. htmlentities <-> html_entity_decode (엔티티

    Date2014.04.12 Views30602
    Read More
  16. PHP에서 자바스크립트 값 가져오기

    Date2014.02.27 Views31640
    Read More
  17. PHP에서 PDF파일 생성하기

    Date2014.02.27 Views32796
    Read More
  18. $_SERVER 환경변수

    Date2016.09.21 Views33242
    Read More
  19. [PHP] 게시판 글쓰기와 이미지 파일 DB 저장 및 불러오기 예제

    Date2017.02.19 Views35901
    Read More
  20. 리다이렉션(페이지 이동)의 3가지 방법, location.href

    Date2017.03.07 Views40468
    Read More
Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved