From 9421ae190da29afc0d5b602e5e99d962cb20f1f3 Mon Sep 17 00:00:00 2001 From: Li Jianxuan Date: Mon, 19 Mar 2012 08:50:13 +0000 Subject: [PATCH] =?UTF-8?q?#284=20=E5=90=8E=E5=8F=B0=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E5=85=83=E6=95=B0=E6=8D=AExml=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controllers/DataController.php | 113 ++++++++++++------ 1 file changed, 74 insertions(+), 39 deletions(-) diff --git a/application/admin/controllers/DataController.php b/application/admin/controllers/DataController.php index b39cd732..3c59030f 100755 --- a/application/admin/controllers/DataController.php +++ b/application/admin/controllers/DataController.php @@ -332,42 +332,77 @@ class Admin_DataController extends Zend_Controller_Action }//编辑附件 + /* + * 输出打包下载的xml文件 + * + * 文件量大时可能出现超时,需要修改超时时间为无限 + */ elseif ($down) { - $zip = new ZipArchive(); - $url="/tmp/xml.zip"; - $opened=$zip->open($url, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE); - if( $opened !== true ){ - die("cannot open {$url} for writing."); - } + + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + //临时zip文件名 + $tmpname="dataxml"; + + //xml文件存放的缓存目录 $dirName = '../data/import/'; - $zip->addEmptyDir("xml/"); - - $filesToAdd = array(); - $dir = dir($dirName); - while (false !== ($node = $dir->read())) { - if (($node == '..') || ($node == '.')) { - continue; - } - if (is_file($dirName.$node)) { - $zip->addFile($dirName.$node,"xml/".$node); - } - } - $zip->close(); - $content=file_get_contents($url); - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); + + //查询需要创建的文件 + $sql = "SELECT md.title,md.uuid,x.* from xml x + LEFT JOIN metadata md ON md.id=x.id"; + + $sth = $this->db->prepare($sql); + $sth->execute(); + $rows = $sth->fetchAll(); //将结果储存,但不使用 + + //创建zip文件,创建成功后再使用查询结果 + $zip = new ZipArchive(); + $url = tempnam($this->config->temp->path,$tmpname);//创建临时文件 + + if( $zip->open($url, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true ) + { + throw new Exception("cannot open {$url} for writing."); + } + + foreach($rows as $k=>$v) + { + + $filename = $dirName.$v['uuid'].'.xml'; + + //创建xml文件 + $handle = fopen($filename,"w"); + fwrite($handle,$v['data']); + fclose($handle); + + //添加到zip文件 + //zip localname 直接使用UUID作为文件名 + $zip->addFile($filename,$v['uuid'].".xml"); + + } + $zip->close(); + + //zip文件创建完成后删除服务器上的缓存文件,防止发生冗余 + foreach($rows as $k=>$v) + { + $filename = $dirName.$v['uuid'].'.xml'; + unlink($filename); + } + + //输出下载 + $content=file_get_contents($url); $this->getResponse()->setHeader('Content-Type', 'application/octet-stream') - ->setHeader('Content-Disposition','attachment; filename="'.basename($url).'"') - ->setHeader('Content-Length', strlen($content)) - ->setHeader('Content-Type','application/force-download') - ->setHeader('Content-Type','application/download') - ->setHeader('Content-Type','application/zip') - ->setHeader('Content-Description','File Transfer') - ->setHeader('Content-Transfer-Encoding','binary') - ->setHeader('Expires',0) - ->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0') - ->setHeader('Pragma','public') - ->setBody($content); + ->setHeader('Content-Disposition','attachment; filename="dataxml.zip"') + ->setHeader('Content-Length', strlen($content)) + ->setHeader('Content-Type','application/force-download') + ->setHeader('Content-Type','application/download') + ->setHeader('Content-Type','application/zip') + ->setHeader('Content-Description','File Transfer') + ->setHeader('Content-Transfer-Encoding','binary') + ->setHeader('Expires',0) + ->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0') + ->setHeader('Pragma','public') + ->setBody($content); }//down else if($search){ @@ -684,11 +719,11 @@ class Admin_DataController extends Zend_Controller_Action $lines=explode("\n",$ref); foreach($lines as $line) { - $data=explode(";",$line); - if (count($data)==2) - $link=''; - else - $link=$data[2]; + $data=explode(";",$line); + if (count($data)==2) + $link=''; + else + $link=$data[2]; $link=$this->db->quote($link); $sql="insert into reference (reference,link) values(?,".$link.")"; try { @@ -1122,7 +1157,7 @@ class Admin_DataController extends Zend_Controller_Action }// 清除元数据来源记录 - elseif ($do=='sync') { //同步数据来源到metadata表 + elseif ($do=='sync') { //同步数据来源到metadata表 $redirect = "/admin/data/source/"; $sql = "update metadata m set source=s.uuid from source s right join datasource d on s.id=d.sourceid where m.uuid=d.uuid"; if($this->db->exec($sql))