phpday 2027 - Call for Papers

ZipArchive::openString

(PHP >= 8.6.0)

ZipArchive::openString — Open a ZIP archive held in a string

説明

public function ZipArchive::openString(string $data = "", int $flags = 0): bool|int

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.

パラメータ

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.

戻り値

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

例

例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'));
?>

上の例の出力は以下となります。

bool(true)
string(6) "Hello
"

参考

+add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top