我记得有发过这个代码,
刚才查看我的主题发现不见了,
现在再发一次。
有服务器的用不上,宝塔就有这个功能。
这个代码适合虚拟主机用。
把这个代码文件放在需要打包压缩的文件夹中,
运行后就可以了。
- <?php
- function getCurrentUrl() {
- $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://';
- $host = $_SERVER['HTTP_HOST'];
- $script = $_SERVER['SCRIPT_NAME'];
- $url = $protocol . $host . $script;
- $url = rtrim($url, '/');
- $url = dirname($url);
- return $url;
- }
- // 作用是压缩同目录下所有文件与文件夹
- function createZip($files = array(), $destination = '', $overwrite = false) {
- if (file_exists($destination) && !$overwrite) {
- return false;
- }
- $zip = new ZipArchive();
- if (is_dir($destination)) {
- return false;
- }
- if ($zip->open($destination, ZipArchive::CREATE) !== true) {
- return false;
- }
- foreach ($files as $file) {
- if ($file != '.' && $file != '..' && file_exists($file)) {
- if (is_dir($file)) {
- addFolderToZip($file, $zip, basename($file));
- } else {
- $zip->addFile($file, basename($file));
- }
- }
- }
- $zip->close();
- return file_exists($destination);
- }
- function addFolderToZip($folder, &$zip, $folderName) {
- $zip->addEmptyDir($folderName);
- $files = scandir($folder);
- foreach ($files as $file) {
- if ($file != '.' && $file != '..') {
- if (is_dir($folder . '/' . $file)) {
- addFolderToZip($folder . '/' . $file, $zip, $folderName . '/' . $file);
- } else {
- $zip->addFile($folder . '/' . $file, $folderName . '/' . $file);
- }
- }
- }
- }
- $filesToCompress = array_diff(scandir('./'), array('.', '..')); // 获取当前目录下的所有文件,排除掉特殊目录
- $destination = 'compressed.zip'; // 压缩文件的目标路径
- if (createZip($filesToCompress, $destination, true)) {
- $downloadUrl = getCurrentUrl() . '/' . $destination;
- echo '文件压缩成功!点击下载:<a href="' . $downloadUrl . '">' . $downloadUrl . '</a>';
- } else {
- echo '文件压缩失败!';
- }
- ?>
复制代码
发帖注意事项
请勿胡乱发帖:https://www.right.com.cn/forum/thread-8307840-1-1.html
账户手机验证:https://www.right.com.cn/forum/home.php?mod=spacecp&ac=plugin&id=jzsjiale_sms:home
|