title: Zip multiple document blob files from MySQL
body: <p>I am trying to take doc,pdf,docx files from MySQL database and zip all the document files together. The problem that I am facing is that I am able to download only one file and unable to ZIP any file as a matter of fact. Please help me in this regard.   </p>
  
  <pre><code>&lt;?php
  
  if(isset($_GET['id']) &amp;&amp; isset($_REQUEST['clickit'])) 
  {
  
      $id = intval($_GET['id']);
  
      if($id &lt;= 0){
          die('The ID is invalid!');
      }else{
  
          $username = "";
          $password = "";
          $servername = "";
          $dbname = "";
  
          $conn = new mysqli($servername, $username, $password,$dbname);
  
  
          if (!$conn){
                  die("Connection failed: " . mysqli_connect_error());
          }
  </code></pre>
  
  <p>Here I have taken all the columns in the table 'tbl_uploads_1' and passed that to result. </p>
  
  <pre><code>        $sql = "SELECT * FROM tbl_uploads_1 ";
  
          $result = $conn-&gt;query($sql);
          $files = array();
  </code></pre>
  
  <p>Here I am taking records from column named 'file_name' from the table in the database which has filenames stored like 'abc.doc'. I am not sure if the code below is the right flow to distinguish between docs with different extensions. </p>
  
  <pre><code>        while($row = $result-&gt;fetch_assoc()) {
              $filename = $row["file_name"];
              $ext1 = end((explode(".", $filename)));
  
              if($ext1 == pdf){
                  $files[] = "$filename.pdf";
              } else if($ext1 == docx){
                  $files[] = "$filename.docx";
              } else if($ext1 == doc){
                  $files[] = "$filename.doc";
              }
  
          //if ( file_put_contents("$filename.pdf", $content) === FALSE ) {
              //echo "Could not write PDF File";
          //}
          }   
  </code></pre>
  
  <p>Here is the zipping part of the code. Please review this.</p>
  
  <pre><code>        $zipfilename = 'temp_' . time() . '.zip';
  
          $zip-&gt;open($zipfilename, ZipArchive::CREATE);
          foreach ($files as $file){ 
             $zip-&gt;addFile($file); 
          } 
          $zip-&gt;close();
  </code></pre>
  
  <p>Should I include header methods for every file before we zip?? </p>
  
  <pre><code>        header('Content-Type: application/zip'); 
          header('Content-disposition: attachment; filename=filename.zip'); 
          header('Content-Length: ' . filesize($zipfilename)); 
          readfile($zipfilename);  
  
  
      }
  //else{
      //echo "Err!!";
  //} 
  
  $conn-&gt;close();
  
  }
  
  exit();
  ?&gt;
  </code></pre>