2014-06-08 29 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