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
"

参见

+添加备注

用户贡献的备注

此页面尚无用户贡献的备注。
To Top