PHP Zip context options

PHPServer Side ProgrammingProgramming

Introduction

PHP's ZIP extension registers zip:// wrapper. PHP 7.2.0 onwards supports passwords for encrypted archives. There is only one Zip context option called password

Example

First create ZIP archive as follows:

<?php
$zip = new ZipArchive;
$zip->open('test.zip');
$zip->setPassword("MySecretPassword");
$zip->addFile('c:/xampp/php/test.txt', 'test.txt');
$zip->close();
>>

To read file from zip:// stream, use following code

<?php
$opts = array(
   'zip' => array(
      'password' => 'secret',
   ),
);
$context = stream_context_create($opts);
echo file_get_contents('zip://test.zip#test.txt', false, $context);
?>
raja
Published on 21-Sep-2020 11:15:01
Advertisements