许多网络主机使用 CPanel,因为它提供了一种相当直观的方式来管理账户。CPanel 自带的文件管理器功能齐全,但有些笨重。尽管 CPanel 提供了一种解压存档文件(.zip 和 .gz)的机制,但该机制有一个主要缺点:解压 .zip 文件时,现有文件不会被覆盖。这使得升级以 .zip 包分发的软件变得非常困难。
为了解决这个限制,我编写了一个 PHP 脚本,允许用户解压 .zip 文件并覆盖现有文件。它向用户显示目录中所有 .zip 文件的列表,并允许他/她提交一个文件进行解压。整个过程非常直观。以下是界面截图:
请选择一个文件解压
然后所选文件将被解压。
要使用该脚本,只需遵循以下步骤:
- 通过 FTP 将此脚本上传(或将脚本粘贴到新文件中)到包含要解压的 .zip 文件的目录中。
- 运行您的网络浏览器,并将其指向该脚本。
- 选择要解压的文件,然后点击“解压”按钮。
就是这样!
请务必在运行后删除该脚本,因为将其留在机器上可能会导致安全问题。
unzip.php
<?php
// The unzip script
// Created by Alex at https://learncpp.com.cn
//
// This script lists all of the .zip files in a directory
// and allows you to select one to unzip. Unlike CPanel's file
// manager, it _will_ overwrite existing files.
//
// To use this script, FTP or paste this script into a file in the directory
// with the .zip you want to unzip. Then point your web browser at this
// script and choose which file to unzip.
// See if there's a file parameter in the URL string
$file = $_GET['file'];
if (isset($file))
{
echo "Unzipping " . $file . "<br>";
system('unzip -o ' . $file);
exit;
}
// create a handler to read the directory contents
$handler = opendir(".");
echo "Please choose a file to unzip: " . "<br>";
// A blank action field posts the form to itself
echo '<FORM action="" method="get">';
$found = FALSE; // Used to see if there were any valid files
// keep going until all files in directory have been read
while ($file = readdir($handler))
{
if (preg_match ("/.zip$/i", $file))
{
echo '<input type="radio" name="file" value=' . $file . '> ' . $file . '<br>';
$found = true;
}
}
closedir($handler);
if ($found == FALSE)
echo "No files ending in .zip found<br>";
else
echo '<br>Warning: Existing files will be overwritten.<br><br><INPUT type="submit" value="Unzip!">';
echo "</FORM>";
?>