Zip file with pass word protection

Discussions of secure PHP coding. Security in software is important, so don't be afraid to ask. And when answering: be anal. Nitpick. No security vulnerability is too small.

Moderator: General Moderators

Post Reply
shanthini
Forum Newbie
Posts: 12
Joined: Wed Jun 18, 2008 11:22 pm

Zip file with pass word protection

Post by shanthini »

Hi

I want to create a zip file with password protection. Using the below code I am able to create a zip file. Can any one help how to protect it with password. :banghead:

$txt_filename="test1.txt";

$zip = new ZipArchive();
$filename = "./data.zip";

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\r\n");
}

$zip->addFile($txt_filename,$txt_filename);
$zip->close();
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Zip file with pass word protection

Post by kaisellgren »

Put code tags around your PHP code.

PHP's native Zip library does not have password support.

You could either make your own Zip class that supports it (search at http://www.phpclasses.org) or you could try something like:

Code: Select all

<?php
echo system('zip -P mypassword thepack.zip thefile.jpg');
?>
I have never done this myself so the above code is purely taken from my head it may not be directly working and if you want to combine many files you have to find your way.
shanthini
Forum Newbie
Posts: 12
Joined: Wed Jun 18, 2008 11:22 pm

Re: Zip file with pass word protection

Post by shanthini »

<?php
echo system('zip -P pass file.zip file.txt');
?>

The above is not working. No errors are displayed simply shows a blank screen and zip file is not created. Pls let me know if I need to include any php library to execute this. If so from where I have to download.

I also searched in phpclasses.org . I am not able to find related class/library.

Please suggest
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Zip file with pass word protection

Post by kaisellgren »

shanthini wrote:<?php
echo system('zip -P pass file.zip file.txt');
?>

The above is not working. No errors are displayed simply shows a blank screen and zip file is not created. Pls let me know if I need to include any php library to execute this. If so from where I have to download.

I also searched in phpclasses.org . I am not able to find related class/library.

Please suggest
It's not a PHP library. It's calling a zip program on the server. You should have this "zip" installed. Basically any zip program is fine, the most popular is http://www.info-zip.org/. A program that is available for both Windows & Linux, supports command line arguments.
shanthini
Forum Newbie
Posts: 12
Joined: Wed Jun 18, 2008 11:22 pm

Re: Zip file with pass word protection

Post by shanthini »

Hi

I used the below code to create zip file. It is working fine when .txt files are added to zip. I am able to do both zip and extract.

But when I am adding .doc files to zip I am able to create .zip file with no issues. When extracting below error occurs. I am using winzip for extraction after saving the .zip to my local machine. This issue occurs only if I try it in online. I am using windows server. When I try the same in my local machine it is working fine.

Please Suggest :banghead:

Error
----
Extracting to "C:\Documents and Settings\Administrator\Local Settings\Temp\"
Use Path: no Overlay Files: yes
Extracting testing.doc
Error: unexpected end of file encountered
Error: invalid compressed data to inflate

Code used for creating .zip file
-----------------------------

Code: Select all

<?
$zip = new ZipArchive();
$filename = "data_test.zip";
$path="d:/consultant_detail/";
 
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE)
exit("cannot open <$filename>\r\n");
 
$zip->addFile($path."test.doc","test.doc");
$zip->close();
?>
Last edited by shanthini on Sat Mar 14, 2009 1:41 am, edited 3 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Zip file with pass word protection

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
Post Reply