平时维护虚拟主机经常遇到不能执行 shell unzip 的情况,这里分享一份带密码验证的 PHP 在线解压脚本,基于 ZipArchive 扩展,不需要调用 exec 等系统命令。
ZipArchive 扩展,大部分 PHP 环境默认自带$password 为自己的密码,防止外人访问安全提醒:该脚本仅适合临时运维使用,不要长期留在线上公开目录,注意目录写入权限,谨慎解压来源不明的压缩包。
完整代码如下:
<?php
// ==========================================
// 在线解压缩脚本 (带密码验证)
// ==========================================
// 设置您的专属登录密码
$password = "YourSecurePasswordHere";
session_start();
// 1. 处理登录请求
if (isset($_POST['login'])) {
if ($_POST['password'] === $password) {
$_SESSION['logged_in'] = true;
} else {
$error = "密码错误,请重新输入。";
}
}
// 2. 处理退出登录
if (isset($_GET['logout'])) {
unset($_SESSION['logged_in']);
header("Location: " . $_SERVER['PHP_SELF']);
exit;
}
// 3. 检查登录状态,未登录则显示登录界面
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>解压缩工具 - 身份验证</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
background-color: #f4f7f6;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.login-card {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
width: 320px;
}
h2 {
text-align: center;
color: #333;
margin-bottom: 20px;
}
.error-message {
color: #d9534f;
font-size: 13px;
margin-bottom: 15px;
text-align: center;
}
.input-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
color: #666;
}
input[type="password"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button {
width: 100%;
padding: 10px;
background-color: #5c67f2;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: background 0.3s;
}
button:hover {
background-color: #4a54e1;
}
</style>
</head>
<body>
<div class="login-card">
<h2>安全验证</h2>
<?php if (isset($error)): ?>
<div class="error-message"><?php echo $error; ?></div>
<?php endif; ?>
<form method="POST">
<div class="input-group">
<label for="password">请输入操作密码:</label>
<input type="password" id="password" name="password" required>
</div>
<button type="submit" name="login">登录</button>
</form>
</div>
</body>
</html>
<?php
exit;
}
// 4. 解压缩业务逻辑
$message = "";
$message_type = "info";
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['unzip'])) {
$zip_file = trim($_POST['zip_file']);
$zip_path = __DIR__ . '/' . $zip_file;
if (empty($zip_file)) {
$message = "请输入文件名。";
$message_type = "error";
} elseif (file_exists($zip_path)) {
// 创建 ZipArchive 实例
$zip = new ZipArchive;
// 尝试打开压缩包
if ($zip->open($zip_path) === TRUE) {
// 解压到当前目录
if ($zip->extractTo(__DIR__)) {
$zip->close();
$message = "文件 <strong>" . htmlspecialchars($zip_file) . "</strong> 成功解压到当前目录!";
$message_type = "success";
} else {
$message = "解压缩失败。请检查目录的写入权限。";
$message_type = "error";
}
} else {
$message = "无法打开 ZIP 文件,请确认文件是否存在且未损坏。";
$message_type = "error";
}
} else {
$message = "未在当前目录下找到指定文件:<strong>" . htmlspecialchars($zip_file) . "</strong>";
$message_type = "error";
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>在线解压缩工具</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
background-color: #f8f9fa;
padding: 40px 20px;
}
.container {
max-width: 600px;
margin: 0 auto;
background: #fff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
border-bottom: 1px solid #eee;
padding-bottom: 15px;
}
h1 {
font-size: 22px;
color: #333;
margin: 0;
}
.logout-btn {
color: #d9534f;
text-decoration: none;
font-size: 14px;
}
.logout-btn:hover {
text-decoration: underline;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 500;
color: #555;
}
input[type="text"] {
width: 100%;
padding: 10px;
border: 1px solid #ced4da;
border-radius: 4px;
box-sizing: border-box;
font-size: 14px;
}
button[type="submit"] {
background-color: #0d6efd;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 15px;
}
button[type="submit"]:hover {
background-color: #0b5ed7;
}
.alert {
padding: 12px 15px;
border-radius: 4px;
margin-bottom: 20px;
font-size: 14px;
}
.alert-success {
background-color: #d1e7dd;
color: #0f5132;
}
.alert-error {
background-color: #f8d7da;
color: #842029;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>在线解压缩工具</h1>
<a href="?logout=true" class="logout-btn">退出登录</a>
</div>
<?php if (!empty($message)): ?>
<div class="alert <?php echo ($message_type === 'success') ? 'alert-success' : 'alert-error'; ?>">
<?php echo $message; ?>
</div>
<?php endif; ?>
<form method="POST">
<div class="form-group">
<label for="zip_file">请输入要解压的 ZIP 文件名称(例如:`archive.zip`):</label>
<input type="text" id="zip_file" name="zip_file" placeholder="请输入文件名" required>
</div>
<button type="submit" name="unzip">立即解压</button>
</form>
</div>
</body>
</html>