简体   繁体   中英

Add file to a zip file with content while it is downloading php

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM