<?php
if (empty($_POST['extraction_directory'])){
// 检查是否有文件被上传
if (isset($_FILES['files']) && count($_FILES['files']['tmp_name']) > 0) {
// 获取所有上传文件的信息
$files = $_FILES['files'];
// 创建一个临时文件用于存储ZIP文件
$zipFile = tempnam(sys_get_temp_dir(), 'compressed_');
// 初始化一个新的ZIP存档
$zip = new ZipArchive();
// 打开ZIP文件
if ($zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
// 遍历上传的文件并添加到ZIP存档中
foreach ($files['tmp_name'] as $index => $tmpPath) {
// 获取文件名
$fileName = $files['name'][$index];
// 添加文件到ZIP存档
$zip->addFile($tmpPath, $fileName);
}
// 关闭ZIP文件
$zip->close();
// 提供ZIP文件供下载
header('Content-Type: application/zip');# 设置响应头
header('Content-Disposition: attachment; filename="compressed_files.zip"');
header('Content-Length: ' . filesize($zipFile));
readfile($zipFile);
// 清理临时文件
unlink($zipFile);
} else {
echo 'Failed to create the ZIP archive.';
}
} else {
echo 'No files were selected for compression.';
}
}else{
if(isset($_FILES['compressed_file'])) {
$file = $_FILES['compressed_file']['tmp_name'];
$destination = $_POST['extraction_directory'];
// 创建目标目录,如果不存在
if (!is_dir($destination)) {
mkdir($destination, 0777, true);
}
$zip = new ZipArchive;
if ($zip->open($file) === TRUE) {
$zip->extractTo($destination);
$zip->close();
echo "ZIP 压缩包解压成功";
} else {
echo "ZIP 压缩包解压失败";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload and Extraction Form</title>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Get references to the elements
var multipleFilesInput = document.getElementById('multipleFiles');
var extractionDirectoryInput = document.getElementById('extractionDirectory');
var compressedFileInput = document.getElementById('compressedFile');
// When the multiple files input changes
multipleFilesInput.addEventListener('change', function() {
if (this.files.length > 0) {
// If files are selected, hide other two inputs
extractionDirectoryInput.style.display = 'none';
compressedFileInput.style.display = 'none';
} else {
// If no files are selected, show the other inputs again
extractionDirectoryInput.style.display = '';
compressedFileInput.style.display = '';
}
});
// When the single compressed file input changes
compressedFileInput.addEventListener('change', function() {
if (this.files.length > 0) {
// If a file is selected, hide the multiple files input
multipleFilesInput.style.display = 'none';
} else {
// If no file is selected, show the multiple files input again
multipleFilesInput.style.display = '';
}
});
});
</script>
</head>
<body>
<form action="../zip/zipht.php" method="post" enctype="multipart/form-data">
<p style="color: red">第一个文件为压缩包文件可以上传多个文件,第二个文件为解压目录只上传一个文件就行了</p>
<input type="file" id="multipleFiles" name="files[]" multiple>
<br />
<input type="file" id="compressedFile" name="compressed_file">
<br />
<input type="text" id="extractionDirectory" name="extraction_directory" value="">
<br />
<input type="submit" value="上传解压或压缩">
</form>
</body>
</html>
07-19
5474
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-20
844
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-20
1715
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)