0

I want to create a weekly backup of my project directory in PHP. I need help to create a password protected zip file in php. I use the below function, but that not create a whole folder backup as well as not helping for password protected.

    $path = realpath('');
    $files = glob($path.'*');
    $files = $files[0];
    $password = "test';
    @system('zip -P $password'.$filename.' '.$files);
Dhara Chauhan
  • 51
  • 1
  • 3
  • 1
    Take a look at the **php ZipArchive** class and the function `$zip->setPassword("YourPasswordHere");` – Richard Jun 22 '17 at 12:02
  • This function only sets the password to be used to decompress the archive; it does not turn a non-password-protected ZipArchive into a password-protected ZipArchive. –  Jun 22 '17 at 12:18
  • I recommend you yo read https://security.stackexchange.com/questions/35818/are-password-protected-zip-files-secure before using zip and password... – Kyslik Jun 22 '17 at 12:27

1 Answers1

0

Using zip command you can try the following.

<?php 
   $path = realpath('');
   $password = "test";
   $zipFileName = "file.zip";
   exec ("zip -r -P {$password} {$zipFileName} {$path}");
?>

-r is added to add all the files recursively.

You can use function system() or exec() to execute the command.

See this post https://unix.stackexchange.com/questions/57013/zip-all-files-in-directory

Saji
  • 1,374
  • 1
  • 12
  • 19