How to create password protected zip files using PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rasana
Forum Newbie
Posts: 9
Joined: Fri Aug 08, 2008 6:07 am

How to create password protected zip files using PHP?

Post by rasana »

Hi All,

How to convert files(i.e. txt, video files, .doc ect) into password protected .zip format on wamp/xamp server?
User avatar
lukewilkins
Forum Commoner
Posts: 55
Joined: Tue Aug 12, 2008 2:42 pm

Re: How to create password protected zip files using PHP?

Post by lukewilkins »

Assuming you are running Linux you can do this in your php:

Code: Select all

$last_line = system('zip -P yourpassword newfile.zip tobezipped.php', $retval);
echo 'Last line of the output: ' . $last_line . '<br/>';
echo 'Return value: ' . $retval . '<br/>';
But please note that this is potentially insecure as Linux may be set up on a multi-user system which can allow other users to see the commands you have ran, and therefore see the password that you set in the command line. This may be really insecure if you are on a shared host and your host has not taken this in to account.

Using -e instead of -P will prompt for your password (and therefore it is not shown to anyone else) but this may not be feasible if you are wanting to run this on the fly in a file (unless you had some sort of expect, but I'm not sure I could help you there).

Hope that helps, and let me know if you have any problems with it.

Luke
rasana
Forum Newbie
Posts: 9
Joined: Fri Aug 08, 2008 6:07 am

Re: How to create password protected zip files using PHP?

Post by rasana »

Thanks for replying
but i'm working on windows.
User avatar
lukewilkins
Forum Commoner
Posts: 55
Joined: Tue Aug 12, 2008 2:42 pm

Re: How to create password protected zip files using PHP?

Post by lukewilkins »

Ah, I see.

I'd recommend this then. http://www.onfocus.com/2007/01/3900
It is a Perl script that is able to handle zipping on a Windows system. Just set-up, pass it a filename and [poof!] you have a zipped file of the same name + .zip

As mentioned in the comments on that page, cygwin is a pretty cool piece of software. I have only played with it a bit, but it basically allows you to run your windows system like it is Linux (Linux commands and all!). I'm sure there is a way to use that and be able to run the first line of code inside the system() function.

Hope that helps!
Luke
Post Reply