2013-06-13 67 views
0

我最近發現PHP的ZipArchive,我沒有任何問題,從字符串中添加文件或文件,但我有一個圖像blob內MySQL數據庫,我想添加到ZipArchive。我可以在一個單獨的文件中獲取圖像,我也可以將它作爲jpg下載。我希望能夠將圖像添加到存檔中。從mysql blob數據庫中添加一個圖像ZipArchive

下面的代碼顯示瞭如何訪問我的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); 

是否有一個人知道我可以如何一起實施它們?

如果您需要任何更多信息的話,我可以提供它:)

回答

2

您可以創建ZipArchive,然後使用addFromString()方法的循環中添加圖像(S)。我從下面的源代碼中使用代碼片段。出於簡單的原因,數據庫連接邏輯被省略。

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

輝煌的工作治療,我想我嘗試了其他組合:) – Martin

+0

@Martin歡迎您 – DevZer0