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 に答える