0

根据这个例子:

<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>

是否也可以将数据写入 .zip 文件?

<?php
$file = 'people.zip';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>
4

1 回答 1

3

创建临时文件并将数据写入其中。

// #1 create tmp data file
$data = 'Hello, World!';
$dataFile = 'file.txt';
file_put_contents($dataFile, $data);

创建 zip 文件并将数据文件放入其中。

// #2 create zip archive
$zip = new ZipArchive();
$zipFile = 'test.zip';
if ($zip->open($zipFile, ZipArchive::CREATE)) {
    $zip->addFile($dataFile, $dataFile);
}
$zip->close();

如果在创建 zip 后不需要,您也可以选择删除数据文件。

// #3 delete
unlink($dataFile);
于 2016-01-07T10:31:29.460 回答