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

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.