【问题标题】:Download files from server in .zip format以 .zip 格式从服务器下载文件
【发布时间】:2016-04-11 11:50:39
【问题描述】:

我想下载我在服务器上上传的文件,使用 PHP 的 .zip 文件。 这些文件没有放在同一个文件夹中。

文件夹命名如下:idClub.'-'.idEvent.'-'.idOrderItem

IdClub 和 idEvent 总是一样的,改变的是 idOrderItem。

我们可以从此查询中获取 orderItem id:

select idOrderItem from order_item when idEvent = $idEvent

$idEvent : 是提供给下载文件的函数的参数。

那么有脚本的例子吗?

【问题讨论】:

标签: php download zip


【解决方案1】:

希望这样的事情有效:

$query= "select idOrderItem from order_item when idEvent = ".$idEvent;
if ($result = $mysqli->query($query)) {
    $zip = new ZipArchive;

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
         $folder_path = '/path/to/idClub-idEvent-'.$row['idOrderItem'].'/';
         if ($zip->open('yourzipname.zip') === TRUE) {
             foreach (glob($folder_path."*.*") as $filename) {
                 $zip->addFile($folder_path.$filename, $filename);
             }
         }
    }
    $zip->close();        
 }

【讨论】:

  • 我不知道文件名,它们应该是在addFile命令中动态的,那么我们该如何解决呢?
  • 你可以使用php中的glob函数来扫描目录并读取文件名。请查看更新后的答案。
猜你喜欢
  • 2021-09-24
  • 2015-08-28
  • 2021-05-29
  • 2017-01-12
  • 2015-10-21
  • 2011-02-09
  • 1970-01-01
  • 1970-01-01
  • 2019-12-12
相关资源
最近更新 更多