php匯出檔案壓縮包 ZipArchive

不言
發布: 2023-03-28 18:58:01
原創
3523 人瀏覽過

這篇文章主要介紹了關於php匯出檔案壓縮套件 ZipArchive,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

    //写入xls文件
    function pro_xls($sContent,$_userid,$sname){
        //生成xls文件
        $exportdir =PHP_ROOT.'data/export/'.$_userid.'/';        
        if(!is_dir($exportdir)) mkdir($exportdir);        
        $elsfile=$exportdir.$sname.'.xls';        
        $file = fopen($elsfile, 'w');
        fwrite($file, $sContent);
        fclose($file);        
        return  $elsfile;

    }    //zip
    function pro_zip($aFiles,$_userid,$sname){
        //zip
        $exportdir =PHP_ROOT.'data/export/'.$_userid.'/';        
        $zipname=$sname.'.zip';        
        $zip_file_path=$exportdir.$zipname;        
        $oZip = new ZipArchive();        
        if($oZip->open($zip_file_path,ZipArchive::CREATE)==TRUE){        
        foreach ($aFiles as $file){            
        $oZip->addFile($exportdir.$file, $file);
        }        
        $oZip->close();        //下载链接
        $downfile="http://".$_SERVER['HTTP_HOST'].'/data/export/'.$_userid.'/'.$zipname;        
        return $downfile;
        }        
        return false;


    }    //csv用  
    function pro_zip_csv($info,$sContent,$_userid,$sname){
        $exportdir =PHP_ROOT.'data/export/'.$_userid.'/';        
        if(!is_dir($exportdir)) mkdir($exportdir);        //生成csv文件
        $elsfile=$exportdir.$sname.'.csv';        
        $fp = fopen($elsfile, 'w');         //Windows下使用BOM来标记文本文件的编码方式 
        fwrite($fp,chr(0xEF).chr(0xBB).chr(0xBF)); 
        foreach ($info as $line) { 
            fputcsv($fp, $line);
        }
        fclose($fp);           

        //zip
        $zipname=time().'.zip';        
        $zip_file_path=$exportdir.$zipname;        
        $oZip = new ZipArchive();        
        if($oZip->open($zip_file_path,ZipArchive::CREATE)==TRUE){            
        $oZip->addFile($elsfile, $sname.'.csv');            
        $oZip->close();            //下载链接
            $downfile="http://".$_SERVER['HTTP_HOST'].'/data/export/'.$_userid.'/'.$zipname;            
            return $downfile;
        }        
        return false;
    }
登入後複製

以上是php匯出檔案壓縮包 ZipArchive的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!