无法下载由ZipArchive动态生成的PHP Zip文件

时间:2018-12-31 08:14:17

标签: php wordpress

我正在尝试创建一个zip文件并在WordPress中单击按钮下载该zip文件,但是当我尝试下载该文件时,它总是会引发错误- 无法修改标头信息-标头已由

发送

这是我的代码-

function downloadZipFile($file_array){
  # create new zip object
  $zip = new ZipArchive();
  # create a temp file & open it
  $tmp_file = tempnam('.', '');
  $zip->open($tmp_file, ZipArchive::CREATE);
  # loop through each file
  foreach ($file_array as $file) {
      //echo $file;
      # download file
      $download_file = file_get_contents($file);

      #add it to the zip
      $zip->addFromString(basename($file), $download_file);
  }
  # close zip
  $zip->close();
  # send the file to the browser as a download
  echo ABSPATH.'downloadZip.php';
  header('Content-disposition: attachment; filename="my file.zip"');
  header('Content-type: application/zip');
  readfile($tmp_file);
  unlink($tmp_file);
}

在上面的函数中,我传递了一个来自box api的文件数组。我可以看到所有文件都来自Box api端点。

0 个答案:

没有答案