메뉴 건너뛰기

프로그램언어

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


  1. 리스트 페이징

    Date2014.02.27 Views19386
    Read More
  2. 필드값 저장

    Date2014.02.27 Views24276
    Read More
  3. 엔터의 표현

    Date2014.02.27 Views19535
    Read More
  4. $_SERVER변수

    Date2014.02.27 Views24444
    Read More
  5. 날짜 계산하기 (하루전 날짜 구하기)

    Date2014.02.27 Views21627
    Read More
  6. 유용한 함수 모음

    Date2014.02.27 Views21422
    Read More
  7. 페이지 로딩 시간 측정

    Date2014.02.27 Views26041
    Read More
  8. PHP에서 자바스크립트 값 가져오기

    Date2014.02.27 Views31635
    Read More
  9. 주간날짜 뽑아오기

    Date2014.02.27 Views26731
    Read More
  10. 로또 숫자 랜덤하게 1~45까지 숫자 빼오기

    Date2014.02.27 Views25433
    Read More
  11. Record Drag/Drop Position

    Date2014.02.27 Views29201
    Read More
  12. 글내용 이미지 리사이징

    Date2014.02.27 Views29446
    Read More
  13. 쿠키변수받기

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

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

    Date2014.02.27 Views28332
    Read More
  16. PHP에서 PDF파일 생성하기

    Date2014.02.27 Views32777
    Read More
  17. PHP로 Excel 파일 만들기...

    Date2014.02.27 Views30257
    Read More
  18. Text를 GD 이미지로 뿌리기

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

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

    Date2014.02.27 Views30280
    Read More
Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved