我有一个创建 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。