PHP 5.4.33 Released

ZipArchive::setPassword

(PHP 5 >= 5.6.0, PECL zip >= 1.12.4)

ZipArchive::setPasswordSet the password for the active archive

Description

public bool ZipArchive::setPassword ( string $password )

Sets the password for the active archive.

Parameters

password

The password to add to the archive.

Return Values

Returns TRUE on success or FALSE on failure.

add a note add a note

User Contributed Notes 1 note

up
0
@mikeziri
4 days ago
$zip = new ZipArchive();
$code = $zip->open('myzip.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
    if ($code === true)
        echo 'zip opened<br>';
    else
        echo $code.'<br>';
    $zip->addFile('somefile');

    $code = $zip->setPassword('secret');

    if ($code === true)
        echo 'password set<br>';
    else
        echo $code.'<br>';

    $code = $zip->close();
    if ($code === true)
        echo 'closed<br>';
    else
        echo $code.'<br>';

    echo 'done<br>';

this prints:

zip opened
password set
closed
done

on the filesystem, the myzip.zip is created with somefile inside but the zip file is not password protected.
To Top