压缩多个数据库 PDF blob 文件
我有一个包含大量 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 PDF 数据存储在数据库中的 BLOB 字段中,我没有看到您将此数据放入文件中。因此,您的 ZIP 将不包含任何文件,因为您没有向其中添加真实的现有文件。
这就是您当前正在执行的操作:
您应该执行的操作:
:
如果这对您有用,您应该做什么:
将文件写入临时文件夹中(也许需要 http://php.net/tempnam) 在其他地方,也许在归档过程后将其删除。
您的代码中还有第二个问题,您使用 $zipfilename 来计算内容长度,使用 $zipname 来读取文件,并且没有变量来创建 zip。由于 $zipfilename 和 $zipname 未定义,因此这不起作用。
您应该使用 ZIP 文件名定义 $zipfilename,并在创建 zip 文件时使用相同的变量。
例子:
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:
This what you should do:
Example:
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:
pdftk 将合并多个 PDF 文件,以及 pdftk 将合并多个 PDF 文件。 pdfsam.org/" rel="nofollow noreferrer">PDFsam..
pdftk will merge multiple PDF files, as well as PDFsam..