Next Galley ... I have hundreds of automatically generated .zip files. These zips files do not have a "default" because they are generated from several different systems. Therefore, some have only XML files, others have other ZIP's inside with other XML's and even folders, so there is not a correct order inside those ZIP's. What I'm fighting: extract all XML's from within these ZIP's with unique names, since it's common for every zip to have ZMLs with the same names, but with different content. In short, it is a recursive extraction ensuring that no file will be replaced with an equal name.
I created the following code:
public function TrataZip($dir)
{
$tmpupload = './data/tmpuploads/' . session_id() . '/';
$files = scandir($dir);
foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
$extensao = pathinfo($path);
if ($extensao['extension'] == 'zip') {
$zip = new \ZipArchive();
$zip->open($path);
$zip->extractTo($dir);
$zip->close();
unlink($path);
return $this->TrataZip($dir);
} elseif ($extensao['extension'] == 'xml') {
$stamp = new \DateTime();
rename($path, $tmpupload . $stamp->format('d-m-Y-H-i-s-') . rand() . "-nfe.xml");
}
} else if ($value != "." && $value != "..") {
return $this->TrataZip($path);
}
}
return $this;
}
The code I've created extracts all the cute little XML files and so on. But then my headache: If I upload multiple ZIP's at the same time and coincidentally these ZIP's have files with the same name, one file will overwrite the other because of the name. This has been my problem and as hard as I try, nothing I use seems to work in Zend Framework 2.
Anyway, regardless of what's inside the ZIP files, whether it's folders, other types of files, etc., I just need ALL XML's and make sure none of them are overwritten by equal names.
Help please