压缩文件并使用PHP中的密码进行保护 [英] ZIP a file and protect with a password in PHP

查看:127
本文介绍了压缩文件并使用PHP中的密码进行保护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码来压缩文件,但我需要使用密码保护该文件

I'm having this code to zip files but i need to protect this file with a password

$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();

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

when i use $zip->setPassword i don't get any errors but the file is not protected at all and when i use ZipArchive::setPassword i get this error "Fatal error: Non-static method ZipArchive::setPassword() cannot be called statically"

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

So how to zip a file in php and protect it with a password?

推荐答案

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

Yes, creation of password protected archives is not supported (they will be created simply as non-protected archives).
But, still it can be used to extract password protected archives.

回到问题.
您总是可以

Returning to the problem.
You always can just

<?php echo system('zip -P pass file.zip file.txt'); ?>

(这将同时在Windows和我们钟爱的Linux上运行)

但是,如果它不符合您的要求,请继续.
我建议您使用 DotNetZip (仅Windows),您将准确地从中动态生成AES加密的zip存档PHP.

(this will work both on Windows and our beloved Linux)

But, if it not fits into your requirements, let's continue.
I would suggest you to use DotNetZip (Windows only), you will exactly dynamically generate AES-encrypted zip archives from PHP.

<?php
// origin: https://stackoverflow.com/a/670804/3684575
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>';
}

?>

但是,这仍然是非常肮脏的解决方案,而更多的解决方案在Linux上不起作用.

所以,尽管PHP是一种成熟的语言,但是没有足够的方法(不包括自定义扩展名或类似的扩展名)来使用纯PHP实现如此简单的任务.
您还可以做的是,等到 PHP 7.2 投入生产(cuz ZipArchive :: setEncryptionName 已实现(感谢Pierre和Remi).
但是,在那之前,您还可以尝试将 php_zip > = 1.14.0移植到PHP< 7.2,但是目前没有可用的已编译二进制文件,因此您必须自己对其进行编译,然后尝试一下是否有可能(我相信这是可行的).
p.s.我会尝试的,但是我的PC上现在没有VS2015 +.

But still, this is very dirty solution and more of that, not works on Linux.

So, although PHP is a mature language, there is no adequate method (excluding custom extension or something like that) to achieve such a simple task with pure PHP.
What you also can do, is to wait until PHP 7.2 will be available for production (cuz ZipArchive::setEncryptionName is implemented (thanks to Pierre and Remi)).
But, until then you also can try to port php_zip >= 1.14.0 to PHP < 7.2, but there is currently no compiled binaries available, so you have to compile it yourself and try if it is possible at all (I believe it is).
p.s. I would try it, but have no VS2015+ on my PC right now.

这篇关于压缩文件并使用PHP中的密码进行保护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆