【发布时间】:2021-04-06 20:04:57
【问题描述】:
我正在尝试在 Internet 上找到的这段代码,它有助于使用 PHP 上的文件创建 ZipArchive。
我正在在我的服务器上尝试这个,这是一个 AWS EC2 with Linux Ubuntu,其中运行 Web 服务器。我运行以下代码:
<?php
function createZipAndDownload($files, $filesPath, $zipFileName)
{
// Create instance of ZipArchive. and open the zip folder.
$zip = new ZipArchive();
$r = $zip->open($zipFileName, ZipArchive::CREATE);
var_dump($r); echo "<br>";
// Adding every attachments files into the ZIP.
foreach ($files as $file) {
$r = $zip->addFile($filesPath . $file, $file);
var_dump($r); echo "<br>";
}
$r = $zip->close();
var_dump($r); echo "<br>";
// Download the created zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename = $zipFileName");
header("Content-length: " . filesize($zipFileName));
header("Pragma: no-cache");
header("Expires: 0");
readfile("$zipFileName");
exit;
}
// Files which need to be added into zip
$files = array('twiglet-1120x720.jpg','22vwqq.jpg','fb4eb7f8aa5431b0b4e26365ebd59933-239x300.jpg');
// Directory of files
$filesPath = 'https://peakon.com/wp-content/uploads/2018/08/';
// Name of creating zip file
$zipName = 'document.zip';
echo createZipAndDownload($files, $filesPath, $zipName);
?>
然后,我有这个输出:
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
我了解 ZipArchive 已创建,但文件未在 ZipArchive 内发送。不过,文件下载完成后,当我想打开它时,写着“文件已损坏”,我无法打开它。
你能帮帮我吗?你知道为什么它不工作吗?我想在里面插入这些图片并下载吗? (图片只是示例)
提前感谢您的帮助。
【问题讨论】:
标签: php upload ziparchive