在 Windows 中使用 PHP 创建受密码保护的 Zip 文件 [英] Creating a Password Protected Zip File using PHP in Windows

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

问题描述

我正在用 PHP 创建一个给定文件的 zip 文件.以下是函数

I am creating a zip file of a given file in PHP. Following is the function

function create_zip($file, $file_name) 
{   
    $zip = new ZipArchive();
    $zip_name = $file_name. ".zip"; // Zip name
    $zip->open($zip_name,  ZipArchive::CREATE);

    if (file_exists($file)) {
        $zip->addFromString(basename($file),  file_get_contents($file));
    } else {
        return "file does not exist";
    }
    $zip->close();

    return $zip_name;   
}

我想为 Zip 文件添加密码保护.我发现以下代码可以创建受密码保护的 zip 文件

I want to add password protection for the Zip files. I have found following code to create a password protected zip file

system('zip -P password file.zip file.txt');  

但是它不能正常工作.你能指导我如何在 Zip 文件上添加密码保护吗?

But it is not working properly. Can you please Guide me How can I add the Password protection on the Zip file?

推荐答案

PHP Zip - http://php.net/manual/en/book.zip.php - 不支持受密码保护的 zip 文件,您需要通过命令行进行操作.

PHP Zip - http://php.net/manual/en/book.zip.php - doesn't support password protected zip files, you will need do it via command line.

要密码保护文件,您需要使用 zip 命令行,确保存在 zip 命令行程序,默认情况下很多系统.

To password protect a file, you need to use zip command line, make sure that the zip command line program is present, it is not installed by default on many systems.

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

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