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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据