如何仅使用 PHP 提取具有密码的 ZIP 文件?
我在这里只看到一个问题,但它没有回答我的问题.我正在运行一个典型的 LAMP 服务器,该服务器具有最新的 PHP 5 和 MYSQL 5 以及 Redhat Linux.
I have seen only one question on here but it does not answer my question. I am running a typical LAMP server that has the most up to date PHP 5 and MYSQL 5 with Redhat Linux.
我需要找到一个仅限 PHP 的解决方案,因为我的主机不允许我使用 shell.
I need to find a PHP only solution because my host does not allow me to use shell.
这是我的代码,用于从 vBulletin 上传中提取未加密的 ZIP 到另一个目录:
Here is my code that extracts ZIPs that are not passworded from vBulletin uploads to another directory:
if ($_GET['add'] == TRUE){
$zip = new ZipArchive;
$res = $zip->open($SOURCE FOLDER);
if ($res === TRUE) {
$zip->extractTo('$DESTINATION FOLDER/');
$zip->close();
echo 'File has been added to the library successfuly';
//Add a flag to that file to indicate it has already been added to the library.
mysql_query("UPDATE attachment SET library = 1 WHERE filedataid='$fileid'");
} else {
echo 'A uncompression or file error has occured';
}}
肯定有某种方法可以只使用 PHP 来做到这一点!谢谢.
There must be some way to do this using just PHP, surely! Thank you.
更新:我的主机通知我服务器上安装了 gzip,但没有安装 7-Zip.我也在研究 shell 访问.
UPDATE: My host informs me that gzip is installed on the server but not 7-Zip. I am looking into shell access too.
推荐答案
为脚本提供 7zip 可执行文件并调用它以通过 system() 使用密码解压缩文件.
Have 7zip executable available to the script and call it to uncompress the file with the password via system().
$strCommandLine = "7z e fileToUnzip.7z -pTHEPASSWORD";
system($strCommandLine);
如果您的主机安装了 unzip
,您可以执行类似的操作.请参阅 http://linux.about.com/od/commands/l/blcmdl1_unzip.htm.
You can do something similar with unzip
, if your host has that installed. See http://linux.about.com/od/commands/l/blcmdl1_unzip.htm.
它支持带密码的-P,所以是这样的:
It supports -P with a password, so something like this:
$strCommandLine = "unzip fileToUnzip.7z -P THEPASSWORD";
system($strCommandLine);
警告:如果有人在系统上执行 ps 并看到您的 unzip 命令正在运行,他们可能会在该命令行上看到您的密码.
Caveat: someone could see your password on that command line if they do a ps on the system and see your unzip command running.
相关文章