找回密码
 注册
搜索
免费空间 免费域名 免费AI 老牌主机商首月仅1美分!27美元/年!Namecheap优惠码Spaceship优惠码
查看: 354|回复: 0

[程序代码] 适用于一般虚拟主机环境的在线解压缩代码

[复制链接]
发表于 2026-5-2 12:08:29 | 显示全部楼层 |阅读模式
直接保存为unzip.php 上传到 服务器,然后打开文件输入密码,输入zip压缩包名称进行在线解压缩。
需要PHP支持Zip,适用于只有FTP权限,没有在线文件管理器的主机,可以同时配合 kalcaddle/KodExplorer: A web based file manager,web IDE / browser based code editor 使用,直接在线安装一个免数据库的文件管理系统。



  1. <?php
  2. // ==========================================
  3. // 在线解压缩脚本 (带密码验证)
  4. // ==========================================

  5. // 设置您的专属登录密码
  6. $password = "YourSecurePasswordHere"; // 改为自己记得住的密码。

  7. session_start();

  8. // 1. 处理登录请求
  9. if (isset($_POST['login'])) {
  10.     if ($_POST['password'] === $password) {
  11.         $_SESSION['logged_in'] = true;
  12.     } else {
  13.         $error = "密码错误,请重新输入。";
  14.     }
  15. }

  16. // 2. 处理退出登录
  17. if (isset($_GET['logout'])) {
  18.     unset($_SESSION['logged_in']);
  19.     header("Location: " . $_SERVER['PHP_SELF']);
  20.     exit;
  21. }

  22. // 3. 检查登录状态,未登录则显示登录界面
  23. if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
  24.     ?>
  25.     <!DOCTYPE html>
  26.     <html lang="zh-CN">
  27.     <head>
  28.         <meta charset="UTF-8">
  29.         <title>解压缩工具 - 身份验证</title>
  30.         <style>
  31.             body {
  32.                 font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  33.                 background-color: #f4f7f6;
  34.                 display: flex;
  35.                 justify-content: center;
  36.                 align-items: center;
  37.                 height: 100vh;
  38.                 margin: 0;
  39.             }
  40.             .login-card {
  41.                 background: white;
  42.                 padding: 30px;
  43.                 border-radius: 8px;
  44.                 box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  45.                 width: 320px;
  46.             }
  47.             h2 {
  48.                 text-align: center;
  49.                 color: #333;
  50.                 margin-bottom: 20px;
  51.             }
  52.             .error-message {
  53.                 color: #d9534f;
  54.                 font-size: 13px;
  55.                 margin-bottom: 15px;
  56.                 text-align: center;
  57.             }
  58.             .input-group {
  59.                 margin-bottom: 15px;
  60.             }
  61.             label {
  62.                 display: block;
  63.                 margin-bottom: 5px;
  64.                 color: #666;
  65.             }
  66.             input[type="password"] {
  67.                 width: 100%;
  68.                 padding: 10px;
  69.                 border: 1px solid #ccc;
  70.                 border-radius: 4px;
  71.                 box-sizing: border-box;
  72.             }
  73.             button {
  74.                 width: 100%;
  75.                 padding: 10px;
  76.                 background-color: #5c67f2;
  77.                 color: white;
  78.                 border: none;
  79.                 border-radius: 4px;
  80.                 cursor: pointer;
  81.                 font-size: 16px;
  82.                 transition: background 0.3s;
  83.             }
  84.             button:hover {
  85.                 background-color: #4a54e1;
  86.             }
  87.         </style>
  88.     </head>
  89.     <body>
  90.         <div class="login-card">
  91.             <h2>安全验证</h2>
  92.             <?php if (isset($error)): ?>
  93.                 <div class="error-message"><?php echo $error; ?></div>
  94.             <?php endif; ?>
  95.             <form method="POST">
  96.                 <div class="input-group">
  97.                     <label for="password">请输入操作密码:</label>
  98.                     <input type="password" id="password" name="password" required>
  99.                 </div>
  100.                 <button type="submit" name="login">登录</button>
  101.             </form>
  102.         </div>
  103.     </body>
  104.     </html>
  105.     <?php
  106.     exit;
  107. }

  108. // 4. 解压缩业务逻辑
  109. $message = "";
  110. $message_type = "info";

  111. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['unzip'])) {
  112.     $zip_file = trim($_POST['zip_file']);
  113.     $zip_path = __DIR__ . '/' . $zip_file;

  114.     if (empty($zip_file)) {
  115.         $message = "请输入文件名。";
  116.         $message_type = "error";
  117.     } elseif (file_exists($zip_path)) {
  118.         // 创建 ZipArchive 实例
  119.         $zip = new ZipArchive;
  120.         
  121.         // 尝试打开压缩包
  122.         if ($zip->open($zip_path) === TRUE) {
  123.             // 解压到当前目录
  124.             if ($zip->extractTo(__DIR__)) {
  125.                 $zip->close();
  126.                 $message = "文件 <strong>" . htmlspecialchars($zip_file) . "</strong> 成功解压到当前目录!";
  127.                 $message_type = "success";
  128.             } else {
  129.                 $message = "解压缩失败。请检查目录的写入权限。";
  130.                 $message_type = "error";
  131.             }
  132.         } else {
  133.             $message = "无法打开 ZIP 文件,请确认文件是否存在且未损坏。";
  134.             $message_type = "error";
  135.         }
  136.     } else {
  137.         $message = "未在当前目录下找到指定文件:<strong>" . htmlspecialchars($zip_file) . "</strong>";
  138.         $message_type = "error";
  139.     }
  140. }
  141. ?>
  142. <!DOCTYPE html>
  143. <html lang="zh-CN">
  144. <head>
  145.     <meta charset="UTF-8">
  146.     <title>在线解压缩工具</title>
  147.     <style>
  148.         body {
  149.             font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  150.             background-color: #f8f9fa;
  151.             padding: 40px 20px;
  152.         }
  153.         .container {
  154.             max-width: 600px;
  155.             margin: 0 auto;
  156.             background: #fff;
  157.             padding: 30px;
  158.             border-radius: 8px;
  159.             box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
  160.         }
  161.         .header {
  162.             display: flex;
  163.             justify-content: space-between;
  164.             align-items: center;
  165.             margin-bottom: 30px;
  166.             border-bottom: 1px solid #eee;
  167.             padding-bottom: 15px;
  168.         }
  169.         h1 {
  170.             font-size: 22px;
  171.             color: #333;
  172.             margin: 0;
  173.         }
  174.         .logout-btn {
  175.             color: #d9534f;
  176.             text-decoration: none;
  177.             font-size: 14px;
  178.         }
  179.         .logout-btn:hover {
  180.             text-decoration: underline;
  181.         }
  182.         .form-group {
  183.             margin-bottom: 20px;
  184.         }
  185.         label {
  186.             display: block;
  187.             margin-bottom: 8px;
  188.             font-weight: 500;
  189.             color: #555;
  190.         }
  191.         input[type="text"] {
  192.             width: 100%;
  193.             padding: 10px;
  194.             border: 1px solid #ced4da;
  195.             border-radius: 4px;
  196.             box-sizing: border-box;
  197.             font-size: 14px;
  198.         }
  199.         button[type="submit"] {
  200.             background-color: #0d6efd;
  201.             color: white;
  202.             border: none;
  203.             padding: 10px 20px;
  204.             border-radius: 4px;
  205.             cursor: pointer;
  206.             font-size: 15px;
  207.         }
  208.         button[type="submit"]:hover {
  209.             background-color: #0b5ed7;
  210.         }
  211.         .alert {
  212.             padding: 12px 15px;
  213.             border-radius: 4px;
  214.             margin-bottom: 20px;
  215.             font-size: 14px;
  216.         }
  217.         .alert-success {
  218.             background-color: #d1e7dd;
  219.             color: #0f5132;
  220.         }
  221.         .alert-error {
  222.             background-color: #f8d7da;
  223.             color: #842029;
  224.         }
  225.     </style>
  226. </head>
  227. <body>
  228.     <div class="container">
  229.         <div class="header">
  230.             <h1>在线解压缩工具</h1>
  231.             <a href="?logout=true" class="logout-btn">退出登录</a>
  232.         </div>

  233.         <?php if (!empty($message)): ?>
  234.             <div class="alert <?php echo ($message_type === 'success') ? 'alert-success' : 'alert-error'; ?>">
  235.                 <?php echo $message; ?>
  236.             </div>
  237.         <?php endif; ?>

  238.         <form method="POST">
  239.             <div class="form-group">
  240.                 <label for="zip_file">请输入要解压的 ZIP 文件名称(例如:`archive.zip`):</label>
  241.                 <input type="text" id="zip_file" name="zip_file" placeholder="请输入文件名" required>
  242.             </div>
  243.             <button type="submit" name="unzip">立即解压</button>
  244.         </form>
  245.     </div>
  246. </body>
  247. </html>
复制代码



您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|免费吧论坛

GMT+8, 2026-11-20 01:37 , Processed in 0.074443 second(s), 3 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表