我有一个创建zip文件的函数。
function zipDoc($docRoot,$archiveName,$testsFolder){
$filename = tempnam($testsFolder, "doc");
$cwd=getcwd();
chdir ($docRoot);
if (is_writeable($docRoot)){
echo $docRoot." is writeable";
}
$zip = new ZipArchive();
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>n");
}
echo "<br/>";
if (is_writeable(dirname($filename))){
echo dirname($filename)." is writeable";
}
$folders = array ("_rels","docProps","word");
// initialize an iterator
// pass it the directory to be processed
foreach ($folders as $folder){
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder."/"));
// iterate over the directory
// add each file found to the archive
foreach ($iterator as $key=>$value) {
if (!is_readable($key)){
echo "File ".$key." not readeble";
}
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}
}
$zip->addFile("[Content_Types].xml");
// close and save archive
echo "<br/>";
if ($zip->close()){
echo $filename." is Closed";
}
else{
echo $filename." is not closed";
}
$newname=str_replace(".tmp",".docx",$filename);
rename($filename,$newname);
chdir($cwd);
return $newname;
}
结果是这两个文件夹都是可写的(我在文件系统上也检查过了),并且zip文件没有关闭!!!有什么建议吗?为什么不关门?编辑:close()方法调用后文件的状态为3670068,函数rename表示该文件正被另一个进程使用。文件已创建,但具有0kb。
好的,我找到了一个解决方案:当将文件夹中的文件添加到zip文件时,我错过了检查它是否是真实的文件,或者只是到当前或父文件夹的链接,例如:/。或/。。在迭代文件时添加了这个简单的检查之后,一切都很好。
if (substr($key,-1)=="."){
continue;
}
希望有人会用这个。