需求
知识点
- php 中有一个类为ZipArchive
- PHP ZIP 扩展已经开启
参考资料
$path = public_path('word');
$zipFileName = 'word.zip';
$zip = new \ZipArchive;
$zip->open($zipFileName, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
foreach ($files as $name =>$file)
{
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = 'word/' . substr($filePath, strlen($path) + 1);
$zip->addFile($filePath, $relativePath);
}
}
$file_name = mb_convert_encoding($zipFileName,'GB2312','UTF-8');
header('Content-type: application/zip');
header("Content-Disposition: attachment; filename=\"{$file_name}\"");
readfile(public_path($file_name));
unlink(public_path($file_name));