'download zip file in php and ajax without location.href

I am creating a zip file download system in php with jquery ajax. When the user clicks the download button, the request goes to the server and returns the filename. I am able to download the file but it shows the name of the entire file along with the file path in console. I do not want this.

<button onclick="checkPayment(this.id)" id="filetoDownload_1">Download File</button>

Javascript

  function checkPayment(data){
    $.ajax({
      type: 'post',
      url: '../download.php',
      data: data,
      async: false,
      success: function(result) {
         let returnkd  = JSON.parse(result);
         if(returnkd["status"]){
             $("#success").html(`<div class="card"><div class="card-body bg-white text-black">Download will start soon...</div></div>`);
             setTimeout(() => {
                  window.location.href = returnkd["filename"];
            }, 2000);
         }else{
            $("#error").html(`<div class="card"><div class="card-body bg-white">Something wents wrong... contact admin</div></div>`);

         }
         
      }
  });
  }

PHP

// wothout header
if(isset($_POST)){
some code
return array("status"=>true,"filename"=>"../folder/filename.zip");

}

// with header
if(isset($_POST)){
some code here
downloadFile($filename);
return array("status"=>true,"filename"=>"../folder/filename.zip");

}

function downloadFile($filename)
    {
        $filename = $filename;
        $filepath = "../".$filename;
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-type: application/octet-stream");
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: " . filesize($filepath));
        ob_end_flush();
        @readfile($filepath);
    }

how to download file without redirecting window.location.href or showing filepath in console. i have tried php header but it doesn't work



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source