aka_bigred Posted December 18, 2007 Share Posted December 18, 2007 I have a simple script that creates a zip archive of all the files in a given folder. Only problem is It puts the directory structure into the zip file. I want to create a zip file with just the files and no foldering. It looks like the ZIPARCHIVE::FL_NODIR flag is something close, but I don't quite understand how to use the flags. IMO the manual is pretty weak on this class. How do I specify multiple flags (ie auto-create & no-folders flags ) Here's my code that buries the files within folders: <?PHP // create object $zip = new ZipArchive(); $thisZipName = 'downloads/prepend_'.$_REQUEST["year"].'-'.$_REQUEST["month"].'.zip'; // open archive ///////////////////////////////////////////////////////////////////////////////////////////// //should I use the ZIPARCHIVE::FL_NODIR flag here when I open too? I also need it to create if not exist ///////////////////////////////////////////////////////////////////////////////////////////// if ($zip->open($thisZipName, ZIPARCHIVE::CREATE ) !== TRUE) { die ("Could not open archive"); } // add all files in directory to archive foreach (glob ('uploads/'.$_REQUEST["year"].'/'.$_REQUEST["month"].'/*.*') as $f) { $zip->addFile(realpath($f)) or die ("ERROR: Could not add file: $f"); } // close and save archive $zip->close(); echo "Archive created successfully."; echo '<BR>Download it <a href="'.$thisZipName.'">Here</a>'; ?> Quote Link to comment Share on other sites More sharing options...
aka_bigred Posted December 18, 2007 Author Share Posted December 18, 2007 Well, I figured it out after a bunch more reading/googling. There's an optional parameter to the addfile() function to specify the local path within zip archive. I just had to change this line $zip->addFile(realpath($f)) to this $zip->addFile(realpath($f),basename($f)) Now it works perfectly! Quote Link to comment 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.