'ZIP'에 해당되는 글 1건

  1. 2009.01.22 PHP 파일 여러개 zip 파일로 묶어서 받기 1
2009. 1. 22. 18:40

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

    // 에러 메시지 뿌리고 history.back()
    function error($msg) {
        echo "<script language=\"javascript\">\r\n";
        echo "    alert(\"".$msg."\");\r\n";
        echo "    history.back();\r\n";
        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;
?>


출처
http://wizard.ncafe.net/wt/?id=all&start=0&no=38&tag=%B4%D9%BF%EE%B7%CE%B5%E5&v_date=&que=

 

Posted by 굿데이