I am trying to extract files from a zip file, but its failing with following error
Warning: copy(zip://upload/myzip-file.zip#myzip-file/file_001.csv): Failed to open stream: operation failed in {code line}
My file myzip-file.zip
is placed inside upload
folder, my code is able to read the contents of this file, but its unable to extract file one by one (I want to extract particular files only. I also want to avoid creation of sub folder)
$zip = new ZipArchive;
if ($zip->open($zipPath) === true) {
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
$fileinfo = pathinfo($filename);
copy("zip://".$zipPath."#".$filename, "my-path/".$fileinfo['basename']);
}
$zip->close();
}
I suspect that copy functoin is not able to understand zip:// I found this sample on net where people have achived same using copy command but its not working for me any more.
Please note
- My php script is at same location as are
upload
andmy-path
(All three in same directory) - My Zip does contain an extra folder
myzip-file
and its confirmed by extracting the full zip contentents and this sinppet$zip->getNameIndex($i);
also revealed that.
Please note you don't have to fix it, but if have any sample which is extracting one single file from zip. It will work for me.