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 :)

图片转代码服务由CSDN问答提供 功能建议

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

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

 标头('  Content-Type:image / jpg; charset = utf-8'); 
 
 
 //创建一个连接到输出流的文件指针
 $ output = fopen('php:// output','w  '); 
 
 $ conB = mysql_connect(“localhost”,“user_name”,“user_pass”); //连接到数据库
if(!$ conB)
 {
 die('无法连接:  '.mysql_error());  //如果无法连接则发送错误消息
} 
mysql_select_db(“binary”,$ conB);  //定义数据库名称
 
 $ id = $ _GET ['ids']; 
 
 $ query = mysql_query(“SELECT * FROM tbl_images WHERE ID ='”。$ id。“'”);  
 
 
 
而($ row = mysql_fetch_array($ query))
 {
 $ content = $ row ['image']; 
 
标题('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”无法打开写作“; 
  } 
 
 
 $ zip-> addEmptyDir('newFolder'); 
 
 $ zip-> addFromString('text.txt','text file'); 
 
 $ zip-&gt  ; close(); 
 
 //然后将标题发送到foce下载zip文件
 
header(“Content-type:application / zip”);  
header(“Content-Disposition:attachment; filename = $ ZipFileName”);  
header(“Pragma:no-cache”);  
header(“Expires:0”);  
 
readfile($ ZipFileName); 
   
 
 

有没有人知道如何一起实现它们?

如果你 需要更多信息然后我可以提供它:)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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();
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题