ZipArchive::openString

Open a ZIP archive held in a string

Description

public boolint ZipArchive::openString(string $data = "", int $flags = 0)

Opens a ZIP archive whose contents are held in a string, instead of in a file. Called without arguments, it starts a new, empty archive in memory. Use ZipArchive::closeString to get the resulting archive back as a string.

Parameters

data
The contents of an existing ZIP archive. The default, an empty string, creates a new archive.
flags
A bitmask of flags, as for ZipArchive::open, for example ZipArchive::CHECKCONS. ZipArchive::EXCL always fails with ZipArchive::ER_EXISTS, as there is no file, and ZipArchive::CREATE has no effect.

Return Values

Returns true on success or the error code on failure, for example ZipArchive::ER_NOZIP if data is not a ZIP archive.

Examples

Example #1 Creating an archive in memory

<?php
$zip = new ZipArchive;
$zip->openString();
$zip->addFromString('hello.txt', "Hello\n");
$bytes = $zip->closeString();

// $bytes can be sent to a client or stored, and read back later.
$zip = new ZipArchive;
var_dump($zip->openString($bytes));
var_dump($zip->getFromName('hello.txt'));
?>

The above example will output:

bool(true)
string(6) "Hello
"

See Also

  • ZipArchive::closeString
  • ZipArchive::open