ZipArchive::closeString

Close the active archive and return it as a string

Description

public stringfalse ZipArchive::closeString()

Closes an archive that was opened with ZipArchive::openString, saves any changes, and returns the resulting archive as a string.

Note: As with ZipArchive::close, a new archive that contains no entries is not written. In that case an empty string is returned, which is not a valid ZIP archive.

Parameters

This function has no parameters.

Return Values

Returns the contents of the archive as a string, or false on failure. When it fails, a warning is raised and the reason is available from ZipArchive::getStatusString.

Errors/Exceptions

Throws an Error if the archive was not opened with ZipArchive::openString, and a ValueError if the archive is not open, for example because it has already been closed.

Examples

Example #1 Building an archive in memory

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

var_dump(is_string($bytes), str_starts_with($bytes, "PK"));
?>

The above example will output:

bool(true)
bool(true)

See Also

  • ZipArchive::openString
  • ZipArchive::close