I have a function which creates a zip file.
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;
}
The output is that both folders are writeable (I checked that on filesystem as well), and that the zip file is not closed!!!
Any suggestions, why is it not closing?
Edit: Status of file after close() method call is 3670068, function rename says that file is being used by another process. The file is created but has 0kb.
OK, I found a solution:
When adding files from folders to a zip-file I missed to check if it is a real file, or just a link to current or parent folder eg.: /. or /..
After adding this simple check when iterating files, everything works fine.
Hope somebody will use this.