I am trying to create a zip file and download that zip file on a button click in WordPress but when I try to download the file it always throws error - Cannot modify header information - headers already sent by
Here is my code -
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);
}
In the above function I am passing a file array which is coming from box api. I can see all files are coming fine from box api endpoint.
