Search code examples
phpzip

backup json file with php?


I have a json files in my project. I will create zip file for donwload by clients. I have write this:

<?php
...
if($_POST['save'])
{
$zip = new ZipArchive;
if ($zip->open('files.zip', ZipArchive::CREATE) === TRUE)
{
    $zip->addFile('data.json');
    $zip->addFile('config.json');
    $zip->close();
}
}
...

After the reopening of the Zip, I got an error. The file files.zip don't exist.

Suppose that these two files are in the same folder.

Is the code that I did wrong? If wrong, please kindly tell me how to fix it.


Solution

  • i have found the solution with getcwd() ;-)

    <?php
    ...
    if($_POST['save'])
    {
    $zip = new ZipArchive;
    if ($zip->open(getcwd().'/files.zip', ZipArchive::CREATE) === TRUE)
    {
        $zip->addFile('data.json');
        $zip->addFile('config.json');
        $zip->close();
    }
    }
    ...