Delete directory structure with zip files in PHP

I ran into a problem when zipping files in PHP. I have a function that zips up an array of files, these files are in different directories. The function looks like this:

function create_zip($files = array(),$destination = '',$overwrite = false,$add_to_db=true) { print_r($files); $zip = new ZipArchive(); if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false; } for($i=0;$i < count($files); $i++) { //echo $files[$i].'<br>'; $zip->addFile($files[$i],$files[$i]); } $zip->close(); if(file_exists($destination)) { // echo("Success"); if($add_to_db == true) { add_file($destination); } return true; } else { //echo("Failed"); return false; } } 

When the user downloads and extracts the zip, the file structure looks like this:

folder / folder2 / file1.jpg
folder / file2.jpg
folder / folder2 / folder3 / file3.jpg

My question is: is it possible for PHP to place all the files in the zip root and ignore this structure. Thus, the extracted files will look like this:

/file1.jpg
/ file 2.jpg
/ file 3.jpg

The only solution I could think of was to move all the files to a folder and then zip this folder, but that seemed redundant.

+7
source share
1 answer

Found an answer in another question

php create zips without file path inside zip

+7
source

All Articles