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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料