I am trying to create an archive of selected files using following code
$zip_in = new \ZipArchive;
if ($zip_in->open('input.zip') === TRUE) {
$files = array();
for ($loop = 0; $loop < $zip_in->numFiles; $loop++) {
$files[] = $zip_in->getNameIndex($loop);
}
$zip_in->extractTo('workspace');
$zip_in->close();
/**
* Changes done to files extracted inside WORKSPACE DIR above
*/
chdir('workspace');
$zip_out = new \ZipArchive();
$zip_out->open('output.zip', \ZipArchive::CREATE);
foreach ($files as $file) {
$zip_out->addFile($file);
}
$zip_out->close();
}
Here I am trying to extract input.zip archive to workspace directory to make changes in files contained inside the zip archive. Following this, I want to again zip all the files back into output.zip archive.
The variable $files
used to store extracted file names is not touched anywhere while editing the files. The file names are hardcoded and not received via any variable.
The last line to finally save the output.zip archive gives following warning on the console:
PHP Warning: ZipArchive::close(): Read error: Is a directory in /home/user/zip.php on line 100
I am unable to understand the reason for this failure. The error message is unclear. What is the reason for this failure ?
Edit:
As requested, added the code to call the archive handler discussed above.
class A {
public function A1 () {
/**
* the code above
*/
}
}
class B {
public function B1 {
$a = new A();
$a->A1();
}
}
This is part of CakePHP 3 app. Taking the hint comments, when I run the code independently outside of CakePHP 3 app it works just fine. However inside the CakePHP app there is some problem. I am adding additional tags to highlight the question in CakePHP community.