add a file to a folder inside a zip file using PHP

11,320

Use PHP's ZipArchive class. Here's the documentation. So you'd do something like:

<?php

$z = new ZipArchive();
$z->open('/path/to/your/file.zip');
// Notice the second argument which specifies the local path in the archive
$z->addFile('/path/to/example.jpg', 'images/example.jpg');
$z->close();

?>

Now your archive has "images/example.jpg".

Share:
11,320
Admin
Author by

Admin

Updated on June 27, 2022

Comments

  • Admin
    Admin almost 2 years

    How can I add a file to a folder inside a zip file using PHP?

    For example if I have the zip file:

    myzip.zip
      |-hello.doc
    

    If I wanted to add the file "example.jpg" inside the "images" folder, the zip file would be:

    myzip.zip
      |-hello.doc
      |-images
        |-example.jpg
    

    How can I achieve this?

  • Facedown
    Facedown almost 10 years
    Your solution create a new folder and in the folder will be folder images with filename example.jpg. Correct solution is without slash on the begin of your path.