此代码运行完美。但是未为.zip文件设置密码。我不知道为什么保存文件时,将其完美保存到zip文件中,但未为该文件设置密码。我认为代码中存在一些错误。
for ($num = 0; $num < count($fileArray["name"]); $num++) {
$fileName = $fileArray["name"][$num];
$tempName = $fileArray["tmp_name"][$num];
move_uploaded_file($tempName, "tmp/" . $fileName);}
$archiveName = rand(10000, 1000000) . ".zip";
$filesArrayNames = $_FILES["upload_files"]["name"];
$zipDir = scandir("zip/");
$error = false;
foreach ($zipDir as $zipDIrFile) {
if ($zipDIrFile == $archiveName) {
$error = true;
break;
}
}
if ($error == false) {
$tmpDir = scandir("tmp/");
$zip = new ZipArchive();
$zip->open("zip/" . $archiveName, ZipArchive::CREATE);
$zip->setPassword($password);
for ($num = 0; $num < count($fileArray["name"]); $num++) {
$fileName = $fileArray["name"][$num];
$zip->setEncryptionName($fileArray["name"][$num], ZipArchive::EM_AES_256);
foreach ($tmpDir as $tmpDirFile){
if($tmpDirFile == $fileName){
$zip->addFile("tmp/".$fileName);
// echo 'Adding'.$fileName."<br />";
echo $password;
}
}
}
$zip->close();
for ($num = 0; $num < count($fileArray["name"]); $num++) {
$fileName = $fileArray["name"][$num];
foreach ($tmpDir as $tmpDirFile){
if($tmpDirFile == $fileName){
unlink("tmp/".$fileName);
}
}
}
} else {
echo 'Name already exists';
}
}