[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1726
  • Last Modified:

how to create zip file on fly

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.

i.e,

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.
0
epdass
Asked:
epdass
  • 3
  • 3
  • 2
  • +1
1 Solution
 
spiffenCommented:
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
die();
?>

Good luck. I never tested this code but I hope you
got some nice ideas..
/Spiffen
0
 
us111Commented:
look spiffen comment:

<?
$ZIPATH="/somewhere";
$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>";
}
else
{     header("Content-Type: application/octet-stream");
     header("Content-Length: ".filesize("$ZIPPATH/$f"));
     header("Content-Disposition: attachment; filename=" .basename("$ZIPPATH/$f"));
     $data="";
     while (!feof($d)) $data.=fread($d,12400);
     fclose($d);
     print $data;
 }

it works
0
 
epdassAuthor Commented:
hi

i will look into it and then i revert you back
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
epdassAuthor Commented:
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.

Cheers
epdass
0
 
MoondancerCommented:
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.

PLEASE DO NOT AWARD THE POINTS TO ME.

____________________________________________



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  
0
 
us111Commented:
waiting
0
 
spiffenCommented:
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.
0
 
MoondancerCommented:
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:
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
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.

Moondancer
Community Support Moderator @ Experts Exchange
0
 
us111Commented:
split the point
0
 
MoondancerCommented:
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.

Moondancer
Community Support Moderator @ Experts Exchange
0

Featured Post

Industry Leaders: 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!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now