1
Next bunch of you... I have hundreds of files . automatically generated zip. These zip files do not have a "standard" as 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 no correct order within these ZIP’s. What I’m struggling with: extract all XML’s from inside these ZIP’s with unique names, because it is common for each zip to have Zmls with equal names, but with different content. In short, it is a recursive extract ensuring that no file will be overwritten for having the same 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 created extracts all the cute XML files and so on. But then my headache: If I upload several ZIP’s at the same time and by coincidence these ZIP’s have files with the same name, one file will overwrite the other due to the name. This has been my problem and no matter how hard I try, nothing I use seems to work in Zend Framework 2.
Anyway, depending on what it contains within ZIP files, whether folders, other types of files and so on, I just need ALL XML’s and make sure that none of them will be overwritten on account of equal names.
Help please