Jump to content

[SOLVED] troubles with creating zip file w/out folders


aka_bigred

Recommended Posts

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>';
?>

 

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.