为了防止任何文件被覆盖,您可能需要先将 zip 文件解压缩到一个目录中。我会创建一个具有随机名称的目录,将 zip 解压缩到该目录中,然后检查是否有任何子目录:
<?php
// Generate random unzip directory to prevent overwriting
// This will generate something like "./unzip<RANDOM SEQUENCE>"
$pathname = './unzip'.time().'/';
if (mkdir($pathname) === TRUE) {
$zip = new ZipArchive;
if ($zip->open('latest.zip') === TRUE) {
$zip->extractTo($pathname);
// Get subdirectories
$directories = glob($pathname.'*', GLOB_ONLYDIR);
if ($directories !== FALSE) {
foreach($directories as $directory) {
$dir_handle = opendir($directory);
while(($filename = readdir($dir_handle)) !== FALSE) {
// Move all subdirectory contents to "./unzip<RANDOM SEQUENCE>/"
if (rename($filename, $pathname.basename($filename)) === FALSE) {
print "Error moving file ($filename) \n";
}
}
}
}
// Do whatever you like here, for example:
unlink($pathname.'installer.php');
}
// Clean up your mess by deleting "./unzip<RANDOM SEQUENCE>/"
}
我尚未测试此代码,因此,使用风险自负,而且它可能无法在 Windows 系统上按预期工作。此外,请查看我使用的所有功能的文档: