Opening and creating password protected zip files with PHP

Adrien Hingert picture Adrien Hingert · Oct 10, 2011 · Viewed 12.5k times · Source

I have found the following two commands to respecively create and open password protected zip files. I was however wondering if it's possible to do this in pure PHP

echo system('zip -P password file.zip file.txt');  
echo shell_exec('unzip -P password file.zip');

Answer

Salvador Dali picture Salvador Dali · Apr 19, 2014

It was not possible do do below PHP5.6.0. But in a new PHP5.6.x version developers added this functionality. So no need for a scary system or shell_exec (which can lead to security vulnerability).

So right now you can do something like this:

ZipArchive::setPassword($password)