2010-06-11 70 views
1

我有一个数据库表,其中包含大量的PDF blob文件。我试图将所有文件合并到一个ZIP文件中,我可以下载并打印。Zip多个数据库PDF blob文件

请帮忙!

<?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(); 


    ?> 
+0

为什么不是你当前的代码工作?你有没有收到任何错误信息? – 2010-06-11 18:07:12

+0

它创建一个空的zip文件。 – Michael 2010-06-11 19:16:06

+0

我想可能是文件路径/目录问题?另外,我是否需要blob条目的标题信息? header(“Content-length:$ size”); header(“Content-type:$ type”); header(“Content-Disposition:inline; filename = $ username- $ description.pdf”); – Michael 2010-06-11 19:20:49

回答

1

你的PDF数据被存储在BLOB字段的数据库中,我没有看到你把这个数据在文件中。所以你的ZIP不会包含任何文件,因为你不会添加真实的现有文件。

这是目前你在做什么:

  • 从数据库
  • 创建文件名
  • 创建文件名的ZIP阵列读你的数据可能不存在

这你应该做什么:

  • 从数据库
  • 创建文件名的阵列中读数据
  • 撰写您BLOB数据到该文件
  • 创建文件存在的ZIP

例子:

$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"; 
     } 
    } 

你应该怎么做,如果这对你有用:

写在临时文件文件夹(可能需要http://php.net/tempnam的帮助),也可能在归档过程中将其删除。

您的代码中还存在第二个问题,您使用$ zipfilename来计算内容长度和$ zipname来读取文件并且没有变量来创建zip。由于$ zipfilename和$ zipname没有定义,所以这不起作用。

您应该使用ZIP文件名定义$ zipfilename,并在创建zip文件时使用相同的变量。

例子:

// 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);