wordpress onpage load auto download zip file

I am try to implement php script for zip file downloading. It work fine when I write below code before get_header(); in wordpress file but page is not open
and when I write code after get_header(); then the page will open but it print the zip file on page. HOW TO FIX THIS ISSUE.

header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath.$filename));
ob_clean();
flush();
readfile($filepath.$filename);
exit;

Related posts