dongnanbi4942 2013-06-13 07:08
浏览 45
已采纳

从mysql blob数据库向ZipArchive添加图像

I have recently discovered ZipArchive for PHP, i have no problem adding files or files from strings however i have an image blob inside a MySQL database that i want to add to the ZipArchive. I can get the image in a separate file and i can also get it to download as a jpg. I want to be able to add the image into the archive.

The below code shows how i access my 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);
    }

That all works fine for me, the below code shows how i am adding a file to a zip archive

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

Does any one know how i can implement them together?

If you need any more information then i can provide it :)

  • 写回答

1条回答 默认 最新

  • dt3358 2013-06-13 07:36
    关注

    You can create the ZipArchive and then add the image(s) from a loop using addFromString() method. I use snippet from both your source codes below. The database connection logic is left out for simplicity reasons.

    $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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch