首页  >  文章  >  后端开发  >  php导出文件压缩包 ZipArchive

php导出文件压缩包 ZipArchive

不言
不言原创
2018-05-31 16:59:383520浏览

这篇文章主要介绍了关于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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn