0

According to this example:

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

Is it also possible to write data into a .zip file?

<?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);
?>
  • You can write data into a file, and put the file(s) into a zip.. – Naruto Jan 07 '16 at 09:58
  • ok, bu tit is not possible to write data directly into the .zip file? –  Jan 07 '16 at 09:59
  • 1
    no, there is another way you can do this, extract file using CLI and get file content append your new content and add new file to zip and replace with old file location. – Ashish Ranade Jan 07 '16 at 10:01

1 Answers1

3

create temporary file and write the data in it.

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

create zip file and put the data file in it.

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

optionally you can also delete the data file if not required after zip is created.

// #3 delete
unlink($dataFile);
Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117