简体   繁体   English

下载php时将文件添加到包含内容的zip文件中

[英]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. 我想将.txt文件添加到包含图片的zip文件中。
while the zip is downloading, I want to add the file .txt without affecting the original file. 下载zip时,我想添加文件.txt而不影响原始文件。

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? 还是唯一的方法是将zip文件解压缩到一个临时文件夹,然后添加txt文件并删除它?

I tried with tmpfile , but it just creates a temporaly file 我尝试使用tmpfile ,但是它只是创建一个临时文件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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