douyin9987 2012-12-15 22:16
浏览 115
已采纳

PHP ZipArchive没有保存

I'm creating a PHP automatic downloader for zipped images. However, the downloaded ZIP is empty! Hopefully somebody will know the issue- it's bound to be simple.

Many thanks, Nick.

PHP

    foreach($_SESSION["photoBasket"] as $id){
       $files[] = array(
               "loc" => "../photos/$id.png",
               "name" => "$id.png"
                );
    }

    $zipname = tempnam(sys_get_temp_dir(), 'zip');
        $zip = new ZipArchive();
        $zip->open($zipname, ZipArchive::CREATE);
        foreach ($files as $file) {
          $zip->addFile($file["loc"],$file["name"]);
          //$zip->addFromString($file["name"],"something here"); WORKS FINE
        }
        $zip->close();

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
        header('Content-Type: application/zip');
        header('Content-Disposition: attachment; filename=test.zip');
    readfile($zipname);
    unlink($zipname);
  • 写回答

1条回答 默认 最新

  • drhgzx4727 2012-12-15 22:32
    关注

    You must create your file in a writeable folder; also, your filename should be unique so that two people downloading a ZIP at the same time don't overwrite each other. Let PHP work out the specifics of that, using tempnam() and sys_get_temp_dir(). And don't forget to remove the temporary file after you've sent it down the wire!

    $zipname = tempnam(sys_get_temp_dir(), 'zip');
    ...
    unlink($zipname);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计