I've got a database table that consists of numerous PDF blob files. I'm trying to mix all the files right into a single ZIP file will be able to download after which print.

Help!

<?php 
    include '../config.php';
    include '../connect.php';

    $session = $_GET['session'];

    $query = "SELECT $tbl_uploads.username, $tbl_uploads.description,
                     $tbl_uploads.type, $tbl_uploads.size, $tbl_uploads.content,
                     $tbl_members.session
    FROM $tbl_uploads
    LEFT JOIN $tbl_members
    ON $tbl_uploads.username = $tbl_members.username
    WHERE $tbl_members.session = '$session'";
    $result = mysql_query($query) or die('Error, query failed');


    $files = array();
    while(list($username, $description, $type, $size, $content) = 
     mysql_fetch_array($result)) {

        $files[] = "$username-$description.pdf";
    }

    $zip = new ZipArchive; 
    $zip->open('file.zip', ZipArchive::CREATE); 
    foreach ($files as $file) { 
      $zip->addFile($file); 
    } 
    $zip->close(); 

    header('Content-Type: application/zip'); 
    header('Content-disposition: attachment; filename=filename.zip'); 
    header('Content-Length: ' . filesize($zipfilename)); 
    readfile($zipname); 

    exit();


    ?>

pdftk will merge multiple PDF files, in addition to PDFsam..

Your PDF information is saved within the database in BLOB fields, I do not help you putting this data inside a file. So that your ZIP won't contain any files since you don't add real existing files into it.

This is exactly what you're presently doing:

  • Reading through your computer data in the database
  • Creating a range of filenames
  • Developing a ZIP of filenames that may not exist

This list of positive actions:

  • Reading through your computer data in the database
  • Creating a range of filenames
  • Write your BLOB data into that files
  • Developing a ZIP of files which exist

Example:

    $files = array();
    while(list($username, $description, $type, $size, $content) = 
     mysql_fetch_array($result)) {
        $files[] = "$username-$description.pdf";

        // write the BLOB Content to the file
        if ( file_put_contents("$username-$description.pdf", $content) === FALSE ) {
          echo "Could not write PDF File";
        }
    }

List of positive actions if which works for you:

Write the files inside a temporary folder (maybe with a few assistance of http://php.net/tempnam) elsewhere and perhaps take them off after your archiving process.

There's additionally a second condition in your code, you utilize $zipfilename for calculating this content-length and $zipname to see the file with no variable to produce the zip. Since $zipfilename and $zipname isn't defined, this will not work.

You need to define $zipfilename having a ZIP filename as well as use that same variable when designing the zip file.

Example:

    // a temp filename containing the current unix timestamp
    $zipfilename = 'temp_' . time() . '.zip';

    $zip->open($zipfilename, ZipArchive::CREATE);
    foreach ($files as $file) { 
       $zip->addFile($file); 
    } 
    $zip->close(); 

    header('Content-Type: application/zip'); 
    header('Content-disposition: attachment; filename=filename.zip'); 
    header('Content-Length: ' . filesize($zipfilename)); 
    readfile($zipfilename);