0

我最近发现了 PHP 的 ZipArchive,我从字符串中添加文件或文件没有问题,但是我在 MySQL 数据库中有一个图像 blob,我想将它添加到 ZipArchive。我可以在单独的文件中获取图像,也可以将其下载为 jpg。我希望能够将图像添加到存档中。

下面的代码显示了我如何访问我的 BLOB

header('Content-Type: image/jpg; charset=utf-8');


// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

$conB = mysql_connect("localhost", "user_name", "user_pass");//connect to the database
if (!$conB)
    {
        die('Could not connect: ' . mysql_error()); // if cannot connect then send error message
    }
mysql_select_db("binary", $conB); // define database name

$id = $_GET['ids'];

$query = mysql_query("SELECT * FROM tbl_images WHERE ID ='".$id."' ");   



while($row = mysql_fetch_array($query))
    {
        $content = $row['image'];

        header('Content-Disposition: attachment; filename=image"'.$row['ID'].'".jpg');

        fwrite($output, $content);
    }

这一切对我来说都很好,下面的代码显示了我如何将文件添加到 zip 存档

$zip = new ZipArchive();
$ZipFileName = "newZipFile.zip";

if ($zip->open($ZipFileName, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true)
    {
        echo "Cannot Open for writing";
    }


$zip->addEmptyDir('newFolder');

$zip->addFromString('text.txt', 'text file');

$zip->close();

//then send the headers to foce download the zip file

header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=$ZipFileName"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 

readfile($ZipFileName);

有谁知道我可以如何一起实施它们?

如果您需要更多信息,我可以提供:)

4

1 回答 1

3

您可以ZipArchive使用方法创建然后从循环中添加图像addFromString()。我使用下面两个源代码的片段。为简单起见,省略了数据库连接逻辑。

$zip = new ZipArchive();
$ZipFileName = "newZipFile.zip";

if ($zip->open($ZipFileName, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true)
{
    echo "Cannot Open for writing";
}


$zip->addEmptyDir('newFolder');

$query = mysql_query("SELECT * FROM tbl_images WHERE ID ='".$id."' ");   

while($row = mysql_fetch_array($query))
{
    $zip->addFromString( $row['image_name'],  $row['image']);
}

$zip->close();
于 2013-06-13T07:36:27.600 回答