繁体   English   中英

PHP使用密码快速创建Zip

[英]PHP Create Zip on the fly with password

尝试动态制作带有密码的Zip文件

<?php
$fileRequest = 'cooking-book';

// Prepare File
$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);

// Stuff with content
$getFile = file_get_contents('http://otherserver.com/getfile.php?sq='.$fileRequest);
$zip->addFromString('cooking-book.txt', $getFile);

// Close and send to users
$zip->close();
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$fileRequest.'.zip"');
readfile($file);
unlink($file);
?>

现在一切正常(将文件从其他服务器下载到内存,将其压缩并发送给用户)。

如何在zip上设置密码? (不能使用exec()shell_exec())它必须在代码中的什么位置?

谢谢。

现在就是未来,现在您可以做到:(PHP7.x功能)

ZipArchive :: setEncryptionName —设置由条目名称http://php.net/manual/de/ziparchive.setencryptionname.php定义的条目的加密方法

<?php
$fileRequest = 'cooking-book';
$passwort = 'PW';

// Prepare File
$file = tempnam("tmp", "zip");
$zip = new ZipArchive();

if ($zip->open($file, ZipArchive::OVERWRITE) === TRUE) {
    // Stuff with content
    $getFile = file_get_contents('http://otherserver.com/getfile.php?sq='.$fileRequest);
    $zip->addFromString('cooking-book.txt', $getFile);
    $zip->setEncryptionName('cooking-book.txt', ZipArchive::EM_AES_256, $passwort);
    // Close and send to users
    $zip->close();
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.$fileRequest.'.zip"');
    readfile($file);
    unlink($file);
} else {
    echo "Error ;)";
}
?>

如果使用的是PHP 5.6,则可以使用ZipArchive :: setPassword将密码添加到当前活动的存档中。

您可以这样使用它:

$zip->setPassword("YourPasswordHere");

我会在$zip->close();之前添加它$zip->close();

暂无
暂无

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

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