douguizhuang8276 2014-04-22 17:20
浏览 22
已采纳

批量选择文件 - Zip文件 - 然后下载到客户端

I can't seem to get anything working from what I am finding when I try to search for a solution with what I need to do.

I am trying to create a form where a user will select 1 or more (bulk select all also) pdf files listed on a webpage.

When the user click submit, I want to have my PHP download file Create a zip file, Add the selected pdf files (store on the server) to the zip file and then force download the zip file to the end user.

When the user opens the zip file, the pdf files they selected from the webpage form are there for their use.

Everything I find either does not work, is not secure or has something to do with uploading the files first which I don't want to do. The files are already stored on my server.

Any help will be much appreciated.

<code>
$files = array($_POST['file']);


        $zip = new ZipArchive();
        $filename = "practiceforms";
        $zip->open($filename,  ZipArchive::CREATE);
        foreach ($_POST['file'] as $key => $val)  {
          echo $path = "/images/docs/solutions/".$val.".pdf";
          if(file_exists($path)){
              $zip->addFile(basename($path),  file_get_contents($path));
          //$zip->addFromString(basename($path),  file_get_contents($path));  
          }
          else{
           echo"file does not exist";
          }
        }
        $zip->close();

        echo header('Content-Length: ' . filesize($filename));
        exit;
</code>
  • 写回答

2条回答 默认 最新

  • douchenbiao0916 2014-04-22 17:39
    关注

    The errors can help:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    

    Try:

    $zip->addFile($path, basename($path));
    

    You might also want the .zip extension:

    $filename = "practiceforms.zip";
    

    Then after sending the proper headers you'll need this to send the file contents to the browser:

    readfile($filename);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论
  • ¥15 keil5 target not created
  • ¥15 C/C++数据与算法请教
  • ¥15 怎么找志同道合的伙伴
  • ¥20 如何让程序ab.eXe自已删除干净硬盘里的本文件自己的ab.eXe文件