1.准备条件:
ZipArchive用来创建和操作压缩包的类
RecursiveDirectoryIterator
和 RecursiveIteratorIterator
是 PHP 中用于递归遍历文件系统目录的类。
这三个类都是内置的类,PHP5以上无需下载。
use ZipArchive;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
2.具体业务代码 。
public static function export($ids){
$idsArr = explode(',', $ids);
$animeData = Db::name("op_anime")->field("id,title,coverpic")->whereIn('id', $idsArr)->select();
$animeChapterData = Db::table("op_anime")->alias("a")
->join("op_anime_chapter b","a.id = b.anid")
->field("b.chaps,b.anid,b.info")
->whereIn('a.id', $idsArr)
->select();
$animeData = $animeData->toArray();
$animeChapterData = $animeChapterData->toArray();
$rootPath = app()->getRootPath();
// 1.创建一个临时目录来存放生成的image文件
$tempDirImage = $rootPath."/"."download".'/image';
if (!is_dir($tempDirImage)) {
mkdir($tempDirImage, 0777, true);
}
//2.创建一个临时目录来存放生成的 TXT 文件
$tempDirCoverpic = $rootPath."/"."download".'/coverpic';
if (!is_dir($tempDirCoverpic)) {
mkdir($tempDirCoverpic, 0777, true);
}
foreach ($animeData as $key => $val){
if($val['coverpic'] != ""){
// 遍历每个,生成对应的图片文件并添加到文件夹
$imageData = @file_get_contents($val['coverpic']);
$imageName = $tempDirImage . "/" . $val['title'] . ".jpg";
file_put_contents($imageName, $imageData);
}
$fp = fopen($tempDirCoverpic."/" . $val['title'].".txt", 'a+');
foreach ($animeChapterData as $key2 => $val2){
if ($val2['anid'] == $val['id']){
if ($val2['chaps'] > 1) {
$content = html_entity_decode($val2['info']);
} else {
$content = html_entity_decode($val2['info']) . '<p>【此处为付费节点】</p>';
}
fwrite($fp, preg_replace("/(\ \;)/", " ", strip_tags(str_replace('</p>', "</p>\r\n", $content))));
}
}
fclose($fp);
}
$rootPath = app()->getRootPath();
// 1.创建一个临时目录来存放生成的image文件
$folderPath = $rootPath."/"."download";
$zipFileName = "download.zip";
// 获取目录中的文件和子目录列表
$files = array_diff(scandir($folderPath), array('..', '.'));
// 如果文件列表为空,则目录就是空的
if (!empty($files)) {
self::createZipAndDownload($folderPath,$zipFileName); die;
} else {
return "没有正确内容可以导出。";
}
}
//$folderPath 文件夹路径
//$zipFileName 压缩包文件名
protected static function createZipAndDownload($folderPath, $zipFileName) {
$zip = new ZipArchive;
if ($zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($folderPath),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($files as $name => $file) {
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($folderPath));
if (is_file($filePath)) {
// 将文件添加到压缩包中,并保留原有目录结构
$zip->addFile($filePath, $relativePath);
} elseif (is_dir($filePath)) {
// 在压缩包中创建目录,并保留原有目录结构
$zip->addEmptyDir($relativePath);
}
}
$zip->close();
//删除目录下文件
self::deleteFiles(app()->getRootPath()."/"."download".'/coverpic/');
//删除目录下文件
self::deleteFiles(app()->getRootPath()."/"."download".'/image/');
// 设置响应头
// $zipFileName = iconv("UTF-8", "GBK", $zipFileName);//加这行防止中文名字的文件乱码
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.$zipFileName.'"');
// 输出压缩包内容
readfile($zipFileName);
// // 删除临时文件
unlink($zipFileName);
} else {
echo '无法创建压缩包';
}
}
protected static function deleteFiles($dir)
{
if(!is_dir($dir)){
return;
}
$handle = opendir($dir);
while($file = readdir($handle)){
if($file != "." && $file != ".."){
$path = $dir ."/" . $file;
if(is_file($path)){
unlink($path);
}
if(is_dir($path)){
self::deleteFiles($path);
}
}
}
closedir($handle);
}