Search code examples
phpzip

Create a zip archive with a password


So, I creating a zip file with a password:

function createZip($fileName,$fileText,$zipFileName,$zipPassword)
    {

       shell_exec('zip -P '.$zipPassword.' '.$zipFileName.'.zip '.$fileName);
       unlink($fileName);
       return file_exists($zipFileName.'.zip');
    }


    $filex = "/backup/home/fyewhzjp/long_location_of_a_file/temp/data/map10/data.txt";
    // $file_content = 'test';
    $archive = "/backup/home/fyewhzjp/long_location_of_a_file/temp/data/map10/archive";

    createZip($filex,$file_content,$archive,$pass);

And it works. I'm getting a archive.zip in my /temp/data/map folder on the website. But, when I open my archive I can see a bunch of folders, and data.txt at the end, let's say it will be /backup/home/fyewhzjp/long_location_of_a_file/temp/data/map10/data.txt So, I need to leave only data.txt in my folder, without other folders. How can I do it?


Solution

  • If anyone will face the same problem as I did, here is the solution: Just add -jrq after zip in shell_exec like this:

    shell_exec('zip -jrq -P '.$zipPassword.' '.$zipFileName.'.zip '.$fileName);
    

    After that, full path will be ignored.