【发布时间】:2011-09-09 18:41:42
【问题描述】:
我正在尝试制作一个 PDf 下载器,用户可以在其中选择几个文件,然后下载他们选择的文件的 zip。我让它在我的个人服务器上工作,但在生产服务器上我得到一个奇怪的错误。生成了 zip 文件,但它附加了一串数字,例如;
zipfile.zip.a08752、zipfile.zip.b08752
更奇怪的是,如果我从末尾删除字符串并下载文件,它会正确展开。
我在这个主题PHP Zip Archive sporadically creating multiple files 中读到,这是文件尝试多次关闭、失败和重试的问题。
这是我的 zip 函数的代码,但我怀疑这与
函数 buildZip($params){
/* 生成唯一 ID */
$downloadid = uniqid();
/* Pull in the order.xml */
if(!empty($_REQUEST['downloadlink'])){
if( $params->usexml == true){
$xml = @simplexml_load_file($params->pdfolder.'/order.xml');
$order = $xml->children();
}else{
$order = $params->files;
}
/* Create the new Zip */
$zip = new ZipArchive();
if ($zip->open($params->zipname.'version'.$downloadid.'.zip', ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
/* Generate the download link to output further down the page */
global $downloadLink;
$downloadLink = $params->zipname.'version'.$downloadid.'.zip';
/* Make selected variable available to build the listSelection function */
global $selected;
$selected = array();
$i = 0;
foreach($order as $el){
if (isset($_POST[$i]) == true){
//generate list of selected PDF's
array_push($selected, $el->name);
//grab selected pdf's and zip them.
echo $zip->addFile($params->pdfolder.'/'.$el->link);
$zip->addFile($params->pdfolder.'/'.$el->link) or die ("ERROR: Could not add file: pdf'.$i.'.html");
}
$i++;
}
$zip->close();
}
}
<code>
为了清晰起见,我引入了一个名为 order.xml 的 XML 列表,以引入可能的文件数组。
【问题讨论】:
-
忘了提,但我们在 PHP 5.2.14 上并且存在 ZIP 类,尽管在 phpinfo() 下,'--enable-zip' 在我的服务器上的配置命令下列出,并且工作正常版本,但不在生产服务器上。不知道这是否有影响。