2014-06-08 34 views
-1

我需要爲我的用戶提供下載所有在我的項目中顯示的圖片的機會。圖像顯示從這樣的一個MySQL查詢:從mysql結果下載圖片

$query = mysql_query("SELECT tl.customername, tl.visitdate, tl.employeename, pz.webpath from table tl 
        inner join pictures pz on pz.visitid = tl.visitid and pz.groupid = tl.groupid 
        inner join agenti ag on ag.idh = tl.employeeid 
        WHERE tl.visitdate >= '$from' AND tl.visitdate <= '$to' 
        AND tl.employeename like '$r_employee' 
        AND tl.customerowner like '$r_customer' 
        AND tl.customername like '$r_customername' 
        AND tl.visitdate like '$r_date' 
        group by pz.webpath order by tl.customername") or die(mysql_error()); 
while($associate = mysql_fetch_assoc($query)) { 
           echo '<li> <figure> 
              <img src="../core/includes/timthumb.php?src='.$associate['webpath'].'&w=200&h=200" /> 
              <figcaption> 
               <h3>'.$associate['customername'].'</h3> 
               <h6>'.$associate['employeename'].'</h6> 
               <h6>'.$associate['visitdate'].' </h6> 
            '; 
           echo '<a class="fancybox" rel="gallery" href="'.$associate['webpath'].'" title=" '.$associate['visitdate'].'/'.$associate['customername'].'">Big picture</i></a>'; 
           echo '</figcaption> 
             </figure> 
            </li>'; 
          $zip->addFromString(pathinfo (urldecode($associate['webpath']), PATHINFO_BASENAME), urldecode($associate['webpath'])); 
          } 

我該如何添加一個下載按鈕,將所有的圖像保存爲zip在用戶計算機上?

+0

什麼的結果呢?實際的圖像數據或只是一個網址? 取決於你有多少用戶/圖像/服務器可能是一個比你期望的更大的問題。如果你給我更詳細的信息,我可以給你一個合適的解決方案 –

+0

用拇指(timthumb) - >點擊 - >大圖(fancybox)返回一個網格中的網址。我需要下載所有在網格中以全分辨率顯示的圖片。 – rozatrra

+0

這是完美的,並且都在同一臺服務器上的圖片?或者你是否也必須從其他地方取他們? –

回答

0

大量閱讀後與凱特和穆斯塔法Torbjørn伯格的幫助下,我設法solveit.So,我的最終代碼看起來like`s這樣的:

<?php 
$files = array(); 
.... 
$query = mysql_query("SELECT tl.customername, tl.visitdate, tl.employeename, pz.webpath from table tl 
        inner join pictures pz on pz.visitid = tl.visitid and pz.groupid = tl.groupid 
        inner join agenti ag on ag.idh = tl.employeeid 
        WHERE tl.visitdate >= '$from' AND tl.visitdate <= '$to' 
        AND tl.employeename like '$r_employee' 
        AND tl.customerowner like '$r_customer' 
        AND tl.customername like '$r_customername' 
        AND tl.visitdate like '$r_date' 
        group by pz.webpath order by tl.customername") or die(mysql_error()); 
while($associate = mysql_fetch_assoc($query)) { 
           echo '<li> <figure> 
              <img src="../core/includes/timthumb.php?src='.$associate['webpath'].'&w=200&h=200" /> 
              <figcaption> 
               <h3>'.$associate['customername'].'</h3> 
               <h6>'.$associate['employeename'].'</h6> 
               <h6>'.$associate['visitdate'].' </h6> 
            '; 
           echo '<a class="fancybox" rel="gallery" href="'.$associate['webpath'].'" title=" '.$associate['visitdate'].'/'.$associate['customername'].'">Big picture</i></a>'; 
           echo '</figcaption> 
             </figure> 
            </li>'; 
     $files[] = $associate['webpath']; 
          } ; 
     $zipname = "download/".$_SESSION["user_name"]."". '-Picture-' ."".time().".zip"; //create zip name using user sesion+Picture+timestamp 
     $zip = new ZipArchive; 
     $zip->open($zipname, ZipArchive::CREATE | ZipArchive::OVERWRITE); // create zip file and overwrite if exist => if timestamp is used, no need for overwrite 
      foreach ($files as $file) { // take each picture from query and insert in zip 
       $zip->addFromString(pathinfo (urldecode($file), PATHINFO_BASENAME), file_get_contents($file)); } // all pictures will be placed in main folder using their original name 
       $zip->close(); //closing zip 
       ... 
    ?>   
// creating download link for the created zip 
<div > <a href ="<?php echo $zipname ?>" > Download pictures </a></div> 
0

您可以使用ZipArchive http://www.php.net/manual/en/class.ziparchive.php用於創建一個ZIP文件,這個比較好:

$files = array(); 
while($associate = mysql_fetch_assoc($query)) 
{ 
//....// 
$files[] = $associate['blob']; 
}; 
$zipname = 'file.zip'; 
$zip = new ZipArchive; 
$zip->open($zipname, ZipArchive::CREATE); 
foreach ($files as $file) { 
    $zip->addFile($file); 
} 
$zip->close(); 
(....) //header 
0

你只需要添加的文件的路徑,您使用的URL,也可能是讀取服務器的錯誤有用登錄以查看問題所在,但此代碼應該適用於您,您基本上遍歷結果並將picturespath列添加到您的zip歸檔文件,讓我知道如果這不起作用!

$zip->open("foo.zip", ZipArchive::CREATE); 

while($associate = mysql_fetch_assoc($query)) { 
    $zip->addFile($associate['picturespath']); 
} 

$zip->close(); 

也確保您緩存ZIP輸出好歹這樣你就可以避免重新創建大量的壓縮文件時,你可以,你至少可以做的僅僅是保存歸檔名稱作爲用戶的ID,並做所有的創作之前檢查你是否有一個與當前用戶的ID第一個壓縮,如果是的話,只是爲他們提供已創建的文件。

+0

謝謝,我finaly根據你的答案和凱特sugestions成功。我會很快發佈我的最終解決方案。 thx再次。 – rozatrra