压缩多个数据库 PDF blob 文件

发布于 2024-09-04 22:18:47 字数 1205 浏览 2 评论 0原文

我有一个包含大量 PDF blob 文件的数据库表。我正在尝试将所有文​​件合并到一个 ZIP 文件中,我可以下载然后打印该文件。

请帮忙!

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


    ?>

I have a database table that contains numerous PDF blob files. I am attempting to combine all of the files into a single ZIP file that I can download and then print.

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


    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咽泪装欢 2024-09-11 22:18:47

您的 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); 

Your PDF data is stored in the database in BLOB fields, I don't see you putting this data in a file. So your ZIP will not contain any files because you don't add real existing files to it.

This is what you are currently doing:

  • Reading your data from the database
  • Creating an array of filenames
  • Creating a ZIP of filenames that might not exist

This what you should do:

  • Reading your data from the database
  • Creating an array of filenames
  • Write your BLOB data into that files
  • Creating a ZIP of files that 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";
        }
    }

What you should do if that works for you:

Write the files in a temporary folder (maybe with some help of http://php.net/tempnam) somewhere else and maybe remove them after your archiving process.

There is also a second problem in your code, you use $zipfilename for calculating the content-length and $zipname to read the file and no variable to create the zip. Since $zipfilename and $zipname is not defined, this won't work.

You should define $zipfilename with a ZIP filename and also use that same variable when creating 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); 
猫卆 2024-09-11 22:18:47

pdftk 将合并多个 PDF 文件,以及 pdftk 将合并多个 PDF 文件。 pdfsam.org/" rel="nofollow noreferrer">PDFsam..

pdftk will merge multiple PDF files, as well as PDFsam..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文