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

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
evibesmusicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Greg WrightSr. Database AdministratorCommented:
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
evibesmusicAuthor Commented:
@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
evibesmusicAuthor Commented:
@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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Greg WrightSr. Database AdministratorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evibesmusicAuthor Commented:
@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
evibesmusicAuthor Commented:
@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
evibesmusicAuthor Commented:
@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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.