ZIP and password protect any file with simple script using PHP

Padlock - Security Image from Prolific Futility
“Rusty Padlock” artwork used with permission from ProlificFutility.com

You may need to password protect a file before sending it to someone.  Yes, there are Windows utilities, some free, that do this but I found this script works surprisingly well as long as “zip” is installed on your server. Just simply save the following file below to your website then upload the file you want to password protect. Be sure to rename “file.txt” in the below example to the name of the file you want to password protect and rename the “file.zip” to the name you want it output as then execute this script in the browser.  All you should have to do is download the ZIP archive version of this file.

<?php

$path=$_SERVER['DOCUMENT_ROOT'].'/pathToThisUtility/';
$path='';

echo system("zip -P pass ".$path."file.zip $path"."file.txt");

?>

When you download the file, you should be prompted with a password before opening.

– Aaron Belchamber

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *