Home / Blog / Post
Password Zip File In Php
Added on 07th June 2019 in Code

In PHP you can open and read ZIP archives, including ones with passwors through the use of ZipArchive, but there are scenarios where you may want to create an archive and add a password. This is not currently supported by ZipArchive but can be achieved by using a cli command executed by php.

Requrements

  • PHP
  • Linux / Unix based host
  • zip command

Code

<?php
    $fileName = "myfile.txt";
    $zipName = "myZip.zip"
    $password = 'mypassword';
    system("zip -P $password $zipName $fileName");

Hi there, I am christopher shaw, a software developer form South Yorkshire, England.

I love tinkering with all things tech, and opened this website as a web presence for myself and a place to keep my notes and snippets. Hopefully you will find something here to help

Christopher Shaw