how to create zip file on fly

Posted on 2001-06-19
Medium Priority
Last Modified: 2008-02-26
hi every body

I am doing programming in php,in which user can able to select the one or more files to download. I want to give the selecting files into zip format to user. How can i do this.


check box: file1
check box: file2
check box: file3
check box: file4

submit -> somename.zip(which has to download to user)

Can anyone help me out.
Question by:epdass
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1

Accepted Solution

spiffen earned 200 total points
ID: 6206712
It depends a bit on what operating system you are running php on. However.. Using the exec() command should to it..

If linux:
exec("zip -0 packedfile.zip file1.txt file2.txt");

If windows:
exec("pkzip -a packedfile.zip file1.txt file2.txt");

Which requires the pkzip to be in PATH..

Writing the whole thing takes too long. But a tip is to
name the checkboxes <input type="checkbox" name="FILE:myfile.txt"> then when submitting (loading the file that sends the file) use:

<?php // file is i.e sendfile.php
$zipfile = "files.zip"; // the name of the zip we create.
while (list($var, $value) = each($HTTP_POST_VARS)) {
 if(substr($var,0,5)=="FILE:") { // is it our checkboxes?
  $tmparr = explode(":",$var); // split at :
  $filename = $tmparr[1]; //filename shouldbe "myfile.txt"
  $filestoadd .= $filename." ";

exec("pkzip -a $zipfile $filestoadd");
header("Content-type: application/download");
header("Content-Length: ".filesize($zipfile));
header("Content-Disposition: inline; filename=$zipfile");
header("Content-Transfer-Encoding: binary");
readfile($filename); // Send it
unlink($filename); // delete it

Good luck. I never tested this code but I hope you
got some nice ideas..

Expert Comment

ID: 6209392
look spiffen comment:

$f = "file.zip";
exec("zip -0 $ZIPPATH/$f file1.txt file2.txt");

$d = @fopen("$ZIPPATH/$f","r");

if (!$d)
{     // If file cannot be read or not available ...
     print "<HTML><BODY><H1>Failed to download $f !!!</H1></BODY></HTML>";
{     header("Content-Type: application/octet-stream");
     header("Content-Length: ".filesize("$ZIPPATH/$f"));
     header("Content-Disposition: attachment; filename=" .basename("$ZIPPATH/$f"));
     while (!feof($d)) $data.=fread($d,12400);
     print $data;

it works

Author Comment

ID: 6210548

i will look into it and then i revert you back
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 6224493
Hi spiffen

Thanks for your nice reply.

I tried your program. It starts download,but arguments are not correctly passed to function. i.e., the zip file is blank. It don't have no files. I checked the path of given files even it's not working. Can you try it for me and send me the feedback. I eagerly waiting for your reply.


Expert Comment

ID: 6419164
It's time to clean up this topic area and that means taking care of this question. Your options at this point are:

1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.

2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

3.  Ask Community Support to help split points between participating experts.  Just comment here with details.

4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.

If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.



Hi Experts:

In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.

Experts, please do not add further "answer" information to this question.  I will be back in about one week to finalize this question.

Thank you everyone.

Moondancer :)
Community Support Moderator @ Experts Exchange

P.S.  REGARDING POINTS, Guidelines and more:  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp  

Expert Comment

ID: 6421055

Expert Comment

ID: 6423915
Oh man. I totally forgot about this topic. :)
Well.. To the question. My example was based on having the files to be zipped in the same directory as the .php doing the exec().. And yes. It worked for me..

You could try to experiment further on with using full path to each file that should be included in the zip.

.. And correct me if Im wrong. But you should have solved the problem by now since it was 2 months ago.. :)

Well.. Sorry about forgetting the topic.
Good luck.

/Regards Spiffen.

Expert Comment

ID: 6699540
ATTN:  epdass

You have asked 43 questions at this site and only closed 26 of them.  You do not remain active with pertinent responses to the experts who have stepped in to help you.  I will be updating all your open questions and monitoring them for closures.  Please do the following:

1)  Add comments to the experts as to your progress and/or what else is needed, if still unresolved .. or ..
2)  Award the expert who helped you and grade them to close the item.  If more than one helped you, open a question here for zero points, include the link and the expert names and amounts you want to award:
3)  Bring any special requests to our attention at the above link that varies from the above processes, or if you have any questions about this site and the guidelines of use.  

Please, again, read the Agreement and site Guidelines, both available from the Help Desk link on the left.  When you signed up for experts-exchange, you agreed to the criteria outlined therein.

Your responsiveness and timely handling of your questions is not only appreciated, but needed.  I will monitor progress for one week, and if still unresolved and inactive will need to decide the outcome.  Expert input is always welcome to determine the outcome.

Community Support Moderator @ Experts Exchange

Expert Comment

ID: 6700188
split the point

Expert Comment

ID: 6700312
Points have been split, one awarded here, the other:

Points for us111 .. qid=20249905  .. this topic area.  Please comment to complete this other half.

Sorry you had to wait so long.

Community Support Moderator @ Experts Exchange

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question