压缩多个数据库PDF blob文件 [英] Zip multiple database PDF blob files
问题描述
我有一个数据库表,其中包含大量的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)或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();
?>
您的PDF数据存储在BLOB字段的数据库,我看不到你把这个数据放在一个文件。因此,您的ZIP不会包含任何文件,因为您不向其中添加真实的现有文件。
这是您当前正在做的:
- 从数据库读取数据
- 创建文件名数组
- 创建可能不存在的文件名的ZIP
您应该这样做:
- 从数据库读取数据
- 创建文件名数组
- 将BLOB数据写入该文件
-
- 创建现有文件的ZIP
示例:
$ files = array();
while(list($ username,$ description,$ type,$ size,$ content)=
mysql_fetch_array($ result)){
$ files [] =$ username- $ description .pdf;
//将BLOB内容写入文件
if(file_put_contents($ username- $ description.pdf,$ content)=== FALSE){
echo无法写PDF文件;
}
}
你应该怎么办: / p>
将文件写入临时文件夹(也许有一些帮助 http ://php.net/tempnam )在其他地方,可能会在您的存档过程后删除它们。
在您的代码中还有第二个问题,使用$ zipfilename计算内容长度和$ zipname读取文件,没有变量创建zip。由于$ zipfilename和$ zipname没有定义,这将不起作用。
你应该用ZIP文件名定义$ zipfilename,并且在创建zip时使用同一个变量文件
示例:
current unix timestamp
$ zipfilename ='temp_'。时间() 。 '。压缩';
$ 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);
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();
?>
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);
这篇关于压缩多个数据库PDF blob文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!