gpt4 book ai didi

php - 使用 php 压缩和下载文件

转载 作者:可可西里 更新时间:2023-11-01 13:04:49 24 4
gpt4 key购买 nike

我正在尝试从名为“上传”的文件夹中压缩和下载文件。Zip 文件正在下载,但我无法打开(提取)它。我收到类似“存档格式未知或已损坏”的错误。

我找到了以下压缩文件夹的代码。

<?php
$files = "upload/".array('Dear GP.docx','ecommerce.doc');
$zipname = 'filename.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=filename.zip');
header('Content-Length: ' . filesize($zipfilename));
readfile($zipname);
?>

最佳答案

感谢您的回答。

<?php
$files = array('Dear GP.docx','ecommerce.doc');

# create new zip opbject
$zip = new ZipArchive();

# create a temp file & open it
$tmp_file = tempnam('.','');
$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=Resumes.zip');
header('Content-type: application/zip');
readfile($tmp_file);
?>

关于php - 使用 php 压缩和下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20162360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com