#284 后台下载全部元数据xml文件

This commit is contained in:
Li Jianxuan 2012-03-19 08:50:13 +00:00
parent bc29229463
commit 9421ae190d
1 changed files with 74 additions and 39 deletions

View File

@ -332,32 +332,67 @@ class Admin_DataController extends Zend_Controller_Action
}//编辑附件 }//编辑附件
/*
* 输出打包下载的xml文件
*
* 文件量大时可能出现超时,需要修改超时时间为无限
*/
elseif ($down) { 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.");
}
$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->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(); $this->_helper->viewRenderer->setNoRender();
//临时zip文件名
$tmpname="dataxml";
//xml文件存放的缓存目录
$dirName = '../data/import/';
//查询需要创建的文件
$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') $this->getResponse()->setHeader('Content-Type', 'application/octet-stream')
->setHeader('Content-Disposition','attachment; filename="'.basename($url).'"') ->setHeader('Content-Disposition','attachment; filename="dataxml.zip"')
->setHeader('Content-Length', strlen($content)) ->setHeader('Content-Length', strlen($content))
->setHeader('Content-Type','application/force-download') ->setHeader('Content-Type','application/force-download')
->setHeader('Content-Type','application/download') ->setHeader('Content-Type','application/download')