【问题标题】:Adding an image to ZipArchive from mysql blob database从 mysql blob 数据库将图像添加到 ZipArchive
【发布时间】:2013-06-13 07:08:47
【问题描述】:

我最近发现了 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);

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

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

【问题讨论】:

    标签: php mysql ziparchive


    【解决方案1】:

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

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

    【讨论】:

    • 很棒的享受,我想我尝试了其他所有组合:)
    • 这个对我也很有效。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-08-27
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多