سؤال

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

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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();
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top