2

I want to add a file .txt to a zip file with images.
while the zip is downloading, I want to add the file .txt without affecting the original file.

so, is possible to do this? or the only way is unzip the zip file to a temporary folder, then add the txt file and delete it?

I tried with tmpfile, but it just creates a temporaly file

Neeox
  • 21
  • 2
  • @Raptor I tried unzipping the zip file to a temporary folder, then add the txt file and delete it; And my question is if the is there another short way to do this – Neeox Jan 02 '15 at 11:38

1 Answers1

0

You can do something like this:

$files = array(
    'file1',
    'file2'
);

$tmp_file = tmpfile();
$zip->open($tmp_file, ZipArchive::CREATE);

# loop through each file
foreach($files as $file){

    # download file
    $download_file = file_get_contents($file);

    #add it to the zip
    $zip->addFromString(basename($file),$download_file);
}

# close zip
$zip->close();

# send the file to the browser as a download
header('Content-disposition: attachment; filename=new-zipfile.zip');
header('Content-type: application/zip');
readfile($tmp_file);
mukund
  • 2,253
  • 1
  • 18
  • 31