我需要让我的用户有机会下载我在项目中显示的所有图像。图像显示在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)
最好使用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
答案 1 :(得分:0)
您只需添加文件的路径,您使用的是URL,读取服务器的错误日志以查看问题也很有用,但此代码应该适用于你,你基本上迭代结果并将picturespath
列添加到你的zip存档中,让我知道这是否有效!
$zip->open("foo.zip", ZipArchive::CREATE);
while( $associate = mysql_fetch_assoc($query)) {
$zip->addFile($associate['picturespath']);
}
$zip->close();
还要确保你以某种方式缓存zip输出,这样你就可以避免重新创建大量的zip文件,至少你可以做的只是将存档名称保存为用户的id,然后再进行所有的创建检查你是否有一个首先是当前用户身份的邮政编码,如果是,那么就把它们提供给已创建的文件。
答案 2 :(得分:0)
经过大量阅读并在Kate和MostafaTorbjørnBerg的帮助下,我设法解决了问题。所以,我的最终代码看起来像这样:
<?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>