压缩文件并使用PHP密码保护

PHP*_*ser 4 php zip password-protection

我有这个代码来压缩文件,但我需要用密码保护这个文件

$file = 'backup.sql';
$zipname = $file.'.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
ZipArchive::setPassword('123456');
//$zip->setPassword("123456");
$zip->addFile($file);
$zip->close();
Run Code Online (Sandbox Code Playgroud)

当我使用$ zip-> setPassword我没有得到任何错误,但文件根本没有受到保护,当我使用ZipArchive :: setPassword我得到此错误"致命错误:非静态方法ZipArchive :: setPassword()不能被称为静态"

那么如何在php中压缩文件并用密码保护它?

小智 11

使用PHP 7.2创建受密码保护的zip文件:

$zip = new ZipArchive;
$res = $zip->open('filename.zip', ZipArchive::CREATE); //Add your file name
if ($res === TRUE) {
   $zip->addFromString('FILENAME_WITH_EXTENSION', 'file content goes here'); //Add your file name
   $zip->setEncryptionName('FILENAME_WITH_EXTENSION', ZipArchive::EM_AES_256, 'PASSWORD'); //Add file name and password dynamically
   $zip->close();
   echo 'ok';
} else {
   echo 'failed';
}
Run Code Online (Sandbox Code Playgroud)


Abr*_*lov 6

是的,不支持创建受密码保护的档案(它们将被简单地创建为不受保护的档案)。
但是,它仍然可以用于提取受密码保护的档案。

回到问题。
你总是可以

<?php echo system('zip -P pass file.zip file.txt'); ?>
Run Code Online (Sandbox Code Playgroud)

(这将适用于 Windows 和我们心爱的 Linux)

但是,如果它不符合您的要求,让我们继续。
我建议您使用DotNetZip(仅限 Windows),您将从 PHP 动态生成 AES 加密的 zip 存档。

<?php
// origin: /sf/answers/46956311/
try
{
  $fname = "zip-generated-from-php-" . date('Y-m-d-His') . ".zip";
  $zipOutput = "c:\\temp\\" . $fname;
  $zipfact = new COM("Ionic.Zip.ZipFile");
  $zip->Name = $zipOutput;
  $dirToZip= "c:\\temp\\psh";
  # Encryption:  3 => 256-bit AES.  
  #     2 => 128-bit AES.  
  #     1 => PKZIP (Weak).  
  #     0 => None
  $zip->Encryption = 3;
  $zip->Password = "AES-Encryption-Is-Secure";
  $zip->AddDirectory($dirToZip);
  $zip->Save();
  $zip->Dispose();

  if (file_exists($zipOutput))
  {
    header('Cache-Control: no-cache, must-revalidate');
    header('Content-Type: application/x-zip'); 
    header('Content-Disposition: attachment; filename=' . $fname);
    header('Content-Length: ' . filesize($zipOutput));
    readfile($zipOutput);
    unlink($zipOutput);
  }
  else 
  {
    echo '<html>';
    echo '  <head>';
    echo '  <title>Calling DotNetZip from PHP through COM</title>';
    echo '  <link rel="stylesheet" href="basic.css"/>';
    echo '  </head>';
    echo '<body>';
    echo '<h2>Whoops!</h2>' . "<br/>\n";
    echo '<p>The file was not successfully generated.</p>';
    echo '</body>';
    echo '</html>';
  } 
}
catch (Exception $e) 
{
    echo '<html>';
    echo '  <head>';
    echo '  <title>Calling DotNetZip from PHP through COM</title>';
    echo '  <link rel="stylesheet" href="basic.css"/>';
    echo '  </head>';
    echo '<body>';
    echo '<h2>Whoops!</h2>' . "<br/>\n";
    echo '<p>The file was not successfully generated.</p>';
    echo '<p>Caught exception: ',  $e->getMessage(), '</p>', "\n";
    echo '<pre>';
    echo $e->getTraceAsString(), "\n";
    echo '</pre>';
    echo '</body>';
    echo '</html>';
}

?>
Run Code Online (Sandbox Code Playgroud)

但是,这仍然是非常肮脏的解决方案,而且更多,不适用于 Linux。

所以,虽然 PHP 是一种成熟的语言,但没有足够的方法(不包括自定义扩展或类似的东西)来用纯 PHP 来实现如此简单的任务。
您还可以做的是等到PHP 7.2可用于生产(因为ZipArchive::setEncryptionName已实现(感谢 Pierre 和 Remi))。
但是,在那之前,您还可以尝试将php_zip >= 1.14.0移植到 PHP < 7.2,但是目前没有可用的已编译二进制文件,因此您必须自己编译它并尝试是否有可能(我相信它是)。
ps 我想试试,但我的电脑上现在没有 VS2015+。