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>'; ?> Link to comment https://forums.phpfreaks.com/topic/82114-solved-troubles-with-creating-zip-file-wout-folders/ 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! Link to comment https://forums.phpfreaks.com/topic/82114-solved-troubles-with-creating-zip-file-wout-folders/#findComment-417593 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.