Question

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

Was it helpful?

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.

OTHER TIPS

Move to the selected folder with chdir:

http://www.php.net/chdir

<?
$zip = new ZipArchive;
$zip->open('newPack.zip', ZipArchive::CREATE);
chdir('2147485934')
foreach (glob("*") as $file) {
$zip->addFile($file);
}
$zip->close();
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top