I want the code to make all files in the tree to .zip files in the root
<?PHP
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open('my-archive.zip', ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
// list of files to add
// list of files to add
$fileList = array(
'im/asd.pdf',
'im/df.pdf',
'im/d/qoyyum.txt'
);
// add files
foreach ($fileList as $f) {
$zip->addFile($f) or die ("ERROR: Could not add file: $f");
}
// close and save archive
$zip->close();
echo "Archive created successfully.";
?>
for example folder /im contains
/im/asd.pdf
/im/df.pdf
/im/d/qoyyum.txt
the ‘my-archive.zip’ extract should look like this
my-archive/asd.pdf
my-archive/df.pdf
my-archive/qoyyum.txt
I want to prevent the folder hierarchy when extracting the zip. so that every files should be in the root of the extracted zip folder
please suggest a tip to do
There’s a second parameter in
ZipArchive::addFile()calledlocalname(see here) which lets you set the local name of the file within the zip archive. Use this to override the directory structure which is the default.