dmikester1 Posted April 10, 2012 Share Posted April 10, 2012 I'm trying to extract the contents of a zip file to a folder. I found the ZipArchive class and followed the examples to get it to work for the most part. But I want to extract the files in the folder inside the zip file but leave the folder out. So it should extract just the files to my given destination. I found this on php.net. If you want to copy one file at a time and remove the folder name that is stored in the ZIP file, so you don't have to create directories from the ZIP itself, then use this snippet (basically collapses the ZIP file into one Folder). <?php $path = 'zipfile.zip' $zip = new ZipArchive; if ($zip->open($path) === true) { for($i = 0; $i < $zip->numFiles; $i++) { $filename = $zip->getNameIndex($i); $fileinfo = pathinfo($filename); copy("zip://".$path."#".$filename, "/your/new/destination/".$fileinfo['basename']); } $zip->close(); } ?> For some reason that 'copy' line is not working for me. Obviosly I've changed the variables in the line to the correct variables. Can someone help me out. Thanks Mike Quote Link to comment https://forums.phpfreaks.com/topic/260694-trying-to-extract-contents-of-zip-file-to-a-folder/ Share on other sites More sharing options...
xyph Posted April 10, 2012 Share Posted April 10, 2012 Using zip:// is something I've never seen done, and is system specific I'd imagine. Why not use ZipArchive::extractTo ? Quote Link to comment https://forums.phpfreaks.com/topic/260694-trying-to-extract-contents-of-zip-file-to-a-folder/#findComment-1336162 Share on other sites More sharing options...
dmikester1 Posted April 10, 2012 Author Share Posted April 10, 2012 I did try the extractTo method and it was extracting the files into a subfolder of the name of the zip file. I want them extracted without that subfolder. Quote Link to comment https://forums.phpfreaks.com/topic/260694-trying-to-extract-contents-of-zip-file-to-a-folder/#findComment-1336164 Share on other sites More sharing options...
xyph Posted April 10, 2012 Share Posted April 10, 2012 I see. I'd use this method then. <?php $zip = new ZipArchive; $destination = 'extracted/'; $zip->open('file.zip'); for($i = 0; $i < $zip->numFiles; $i++) { $name = basename($zip->getNameIndex($i)); $data = $zip->getFromIndex($i); file_put_contents($destination.$name, $data); } ?> Keep in mind, it will over-write files with the same name, in different folders within the zip file. Quote Link to comment https://forums.phpfreaks.com/topic/260694-trying-to-extract-contents-of-zip-file-to-a-folder/#findComment-1336167 Share on other sites More sharing options...
dmikester1 Posted April 10, 2012 Author Share Posted April 10, 2012 Sweet! That worked perfectly. Thank you very much! Mike Quote Link to comment https://forums.phpfreaks.com/topic/260694-trying-to-extract-contents-of-zip-file-to-a-folder/#findComment-1336185 Share on other sites More sharing options...
xyph Posted April 10, 2012 Share Posted April 10, 2012 You should also wipe $data using unset($data) after the loop. Though I don't know exactly how PHP handles large variables left in memory, I know it's good practise to destroy potentially large variables after use. Perhaps someone with more intricate knowledge could help further. Quote Link to comment https://forums.phpfreaks.com/topic/260694-trying-to-extract-contents-of-zip-file-to-a-folder/#findComment-1336188 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.