Search code examples
phpzipphp-ziparchive

Copy folder content to Zip php


I have the next folder with files:

2147485934: {image.ai, image.jpg, image.psd}

And I want to create a new zip with thoses files:

<?
$zip = new ZipArchive;
$zip->open('newPack.zip', ZipArchive::CREATE);
foreach (glob("2147485934/*") as $file) {
$zip->addFile($file);
}
$zip->close();
?>

It works, but the new zip file have the same structure:
newPack.zip --> 2147485934: {image.ai, image.jpg, image.psd}
and I want to get the next structure inside my zip:
newPack.zip --> image.ai, image.jpg, image.psd //without folder 2147485934

Any help with this problem would be much appreciated


Solution

  • Try this:

    $zip->addFile($file, basename($file));
    

    The second argument defines the name of the file inside the archive. The code I added strips the directory name from the filename inside the archive.