[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How to create a zip file in php, add files dynamically, then download immediately?

Posted on 2010-03-30
7
Medium Priority
?
319 Views
Last Modified: 2013-12-12
Experts,

I am trying to use form results to create a new zip file, add files to it dynamically, then download the newly created and packed zip file.

I think that I am on the right track but, can't figure it out.  Any help would be great.  Thanks.

if($_GET['cmd']=="zip"){

      $chosen_zips = $_POST['zip'];
      $chosen_ids = $_POST['id'];
      
            $sql = "SELECT * FROM permmed_reports WHERE id='$chosen_ids'";
            $data = mysql_query($sql);
                  
            $zip = new ZipArchive;
                  while($results = mysql_fetch_array($data)){
                        $selected_file = 'reports/'.$results['Year'].'/'.$results['Department'].'/'.$results['Site'].'/'.$results['File'].'';
                        $zip->addFile(''.$selected_file.'', ''.$results['File'].'');
                        }
                  $zip->close();


}//IF STATEMENT
0
Comment
Question by:evibesmusic
  • 5
  • 2
7 Comments
 
LVL 2

Expert Comment

by:Greg Wright
ID: 29162322
Why do you say "you can't figure it out"? Is there an error? Is the file created?

It appears you haven't instantiated (to open) the zip file... $zip->open() ... see below.

Add the following:
$zip = new ZipArchive;
$zip->open('$chosen_zips')
while($results = mysql_fetch_array($data))
{
    $selected_file = 'reports/'.$results['Year'].'/'.$results['Department'].'/'.$results['Site'].'/'.$results['File'].'';
    $zip->addFile(''.$selected_file.'', ''.$results['File'].'');
}
$zip->close();

Is the folder valid where you are picking up the text file for $selected_file?

Here are some links that may assist you further.

http://davidwalsh.name/create-zip-php
http://php.net/manual/en/function.ziparchive-addfile.php
0
 

Author Comment

by:evibesmusic
ID: 29236445
@ALL:

Sorry for the vague description of the problem...late night.

Background: I have a dynamically created form.  This form uses checkboxes so the user can designate which files they would like zipped.

I have confirmed that the form is passing the correct information (db column 'id') to the code below.  My problem now is that I don't believe that the code to create a brand new, empty zip file, then add files to it, close it, and then present it to the user for download works.

This is the first time I have attempted to use this feature in php.  I don't know if in the code below does, 'new.zip' actually have to exist on the server, or is this the name given to the zip file once it has been stuffed with files?

Any help would be greatly appreciated.


if($_GET['cmd']=="zip"){

// ENSURE THAT PHP SESSION HAS BEEN STARTED                              

      foreach($_POST as $key => $value) {
            $row_number = $key; // $row_number EQUALS THE KEY STORED IN THE ARRAY
            
            $sql="SELECT * FROM permmed_reports WHERE id='$row_number'";
            $data = mysql_query($sql);
            $zip = new ZipArchive;
            $zip->open('new.zip');
            while($result = mysql_fetch_assoc($data)){      
                  $selected_file = 'reports/'.$result['Year'].'/'.$result['Department'].'/'.$result['Site'].'/'.$result['File'].'';
                  $zip->addFile(''.$selected_file.'', ''.$result['File'].'');
            }//END WHILE LOOP
            $zip->close();
      }//END FOREACH LOOP

}//IF STATEMENT
0
 

Author Comment

by:evibesmusic
ID: 29237613
@ALL:

Is there an easy way to tell if zip files are enabled by looking at the content of my php.ini?

I understand that I have to have zip extensions enabled on my server but, only have access to the php.ini file at the moment.

Just wanted to start at square one and make sure that I am not getting any issues because the process is not currently enabled.

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 2

Accepted Solution

by:
Greg Wright earned 2000 total points
ID: 29241911
You do need to have the PHP compiled with zip capabilities. See the following for extended details on this. http://www.php.net/manual/en/zip.installation.php

Looking in the php.ini can help you to know if it's enabled, which you will have to do to use it.

Here are the procedures.
http://www.w3schools.com/php/php_ref_zip.asp
0
 

Author Comment

by:evibesmusic
ID: 29245669
@snapjag:

Thanks.  Looking at whether the support is installed correctly, and whether or not the process is enabled within my php.ini file.  Thanks...standby.
0
 

Author Comment

by:evibesmusic
ID: 29247584
@snapjag:

OK...my server admin just installed the dll and enabled the ini to support it.  Let me see if this will work now...brb.
0
 

Author Comment

by:evibesmusic
ID: 29249490
@snapjag:

Ok, I've tested the following code:

if($_GET['cmd']=="zip"){
      foreach($_POST as $key => $value) {
            $row_number = $key; // $row_number EQUALS THE KEY STORED IN THE ARRAY
            $sql="SELECT * FROM permmed_reports WHERE id='$row_number'";
            $data = mysql_query($sql);
                  if($data){
                  //CREATE ZIP FILE, ADD THE FILES SELECTED BY THE USER BASED ON THE ID PASSED BY THE FORM, CLOSE NEWLY CREATED ZIP AND PRESENT IT TO THE USER
                  $zip = new ZipArchive;
                  $zipname = 'PermMedReports_Files.zip';
                        while($result = mysql_fetch_assoc($data)){      
                              $selected_file = 'reports/'.$result['Year'].'/'.$result['Department'].'/'.$result['Site'].'/'.$result['File'].'';
                              $zip->addFile($selected_file, .$result['File'].);
                        }//END WHILE LOOP
                  $zip->close();
                  $zip->output ($zipname);
                  } else { echo '<div class="controllers_message"><b>ERROR</b><br /><br />Sorry.  Your zipped file was not created.  Please press here to continue.</div>';}
      }//END FOREACH LOOP
}//IF STATEMENT

The page does not render correctly but, I get no error messages.  I have commented and tested the code line-by-line, and when the code gets to the if($data){...} statement, it fails...if I take the code within the if($data) statement out, the page renders correctly.

Do you know where the error may be?
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

612 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