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条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler