1

I have some textstrings in memory and want to create a simple ZIP from it. Do I have to create a textfile and store it on the server first or can I create a ZIP directly from memory and store it finally. This would save time and action.

3
  • 2
    php.net/manual/en/ziparchive.addfromstring.php Commented Oct 10, 2016 at 12:53
  • 6
    I am pretty sure that manual is not closed today... php.net/manual/en/ziparchive.addfromstring.php Commented Oct 10, 2016 at 12:53
  • Hi guys - I was reading the manual. Obviously must have overread this doe to a lot of ZIP SO answers relating to add files from disk. Thanks to people downvoting, very tolerant ! Commented Oct 10, 2016 at 12:58

1 Answer 1

3

You can use addFromString() from ZipArchive.

Example very much alike the one from manual:

<?php
    $content = 'My content in Memory';

    $zip = new ZipArchive;
    $res = $zip->open('archive.zip', ZipArchive::CREATE);
    if ($res === TRUE) {
        $zip->addFromString('file.txt', $content);
        $zip->close();
    } else {
       echo 'Error: Zip couldn\'t be created.';
    }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

@Ben : Did you find another solution or did this respond to your question?
I use and accept your answer. Works perfect. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.