繁体   English   中英

如何使用 php 在 ftp 服务器上创建受密码保护的 zip 文件

[英]How to create a password protected zip file on ftp server using php

我想使用 php 在 ftp 服务器上创建一个受密码保护的 zip 文件。 我已经尝试了下面的代码,但它不起作用。 下面的代码可以在本地工作,但是当我放在 ftp 服务器上时它不起作用。 我在本地有一个 client.php,在 ftp 服务器中有一个 server.php。 我把下面的代码放在 server.php.( zipArchive 或 7-zip 都可以接受)

(下面的代码不包括创建密码功能。它只是创建一个 zip 文件而已。)

$zip = new ZipArchive;
if ($zip->open('Ftp://user.com/new/temp.zip', 
 ZipArchive::CREATE) === TRUE)
{
    // Add files to the zip file
    $zip->addFile('Ftp://user.com/new/temp/*');

    // All files are added, so close the zip file.
    $zip->close();
    echo"Create Successful";
}

预期输出:

受密码保护的 zip 文件创建成功。

实际输出:

在 ftp 服务器中没有创建 zip 文件。

<?php $target_dir = "uploads/"; $target_file = $target_dir . basename( $_FILES[ "fileToUpload" ][ "name" ] ); if ( isset( $_POST[ "submit" ] ) ) { if ( move_uploaded_file( $_FILES[ "fileToUpload" ][ "tmp_name" ], $target_file ) ) { echo "The file " . basename( $_FILES[ "fileToUpload" ][ "name" ] ) . " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } ?>

这是仅创建 zip 文件的代码。 我不知道如何使用ZipArchive()方法实现 zip 加密

您可能需要使用setEncryptionName()方法来实现此目的

或者前往http://php.net/manual/en/ziparchive.setencryptionname.php

试试下面的代码

<?php 
$zip = new ZipArchive(); $zipFile = __DIR__ . '/output.zip'; if (file_exists($zipFile)) { unlink($zipFile); } $zipStatus = $zip->open($zipFile, ZipArchive::CREATE); if ($zipStatus !== true) { throw new RuntimeException(sprintf('Failed to create zip archive. (Status code: %s)', $zipStatus)); } $password = 'top-secret'; if (!$zip->setPassword($password)) { throw new RuntimeException('Set password failed'); } // compress file $fileName = __DIR__ . '/test.pdf'; $baseName = basename($fileName); if (!$zip->addFile($fileName, $baseName)) { throw new RuntimeException(sprintf('Add file failed: %s', $fileName)); } // encrypt the file with AES-256 if (!$zip->setEncryptionName($baseName, ZipArchive::EM_AES_256)) { throw new RuntimeException(sprintf('Set encryption failed: %s', $baseName)); } $zip->close();
<?php>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM