0

I create a zip file with php with this code:

<?php
$file1 = '/home/##ACCOUNT##/public_html/##SITE##/images/uploadfiles/backup/file1.csv';
$file2 = '/home/##ACCOUNT##/public_html/##SITE##/images/uploadfiles/backup/file2.csv';

// CREATE THE ZIP
$files = array($file1, $file2);
$zipname = '/home/##ACCOUNT##/public_html/##SITE##/images/uploadfiles/backup/backup.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
?>

In the created zip i get 2 files as i want it but it also contains the tree/path of those files so opening the zip you have to navigate inside those folder to get to the files is there a away to just have the 2 files in the zip file1.csv & file2.csv without the folders?

I found the anwser i wanted in another post php creating zips without path to files inside the zip

Community
  • 1
  • 1
John
  • 227
  • 3
  • 16

2 Answers2

0

You can use ZipArchive::renameName method to change name of the file being added or use ZipArchive::addFromString. In the second case you need to remember that you first need to read that file into memory, so it should only be used in consideration with memory available.

Also you can use second parameter to ZipArchive::addFile as suggested in other answer.

foreach ($files as $file) {
    $localName = basename($file);

    $zip->addFile($file, $localname);
}
baldrs
  • 2,132
  • 25
  • 34
0

As Workaround use $localname (second parameter) to define and control file/directory structure inside the zip eg.

<?php 
     $zip->addFile($file, $localname_relative_path); 
?>
donald123
  • 5,638
  • 3
  • 26
  • 23