How to Use PHP headers() in Wordpress to Download Files from my Custom Plugin Page? : <!-- What are you trying to accomplish? (Please include sample data.) -->
I am Trying to Use **PHP headers() in wordpress to download files from my plugin page view** but is not working as this Error Message return for each header line present in my script:

> Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\wordpress\wp-includes\formatting.php:5100) in D:\xampp\wordpress\wp-content\plugins\pxw-test\pxw-test.php on line 33

and so on for each line of present headers(); in my script..

<!-- Paste the part of the code that shows the problem. (Please indent 4 spaces.) -->
**In order to replicate my Errors Download The Test Plugin named " pxw-test " :**

[pxw-test.zip][1] > https://ufile.io/ld2v4

**The plugin contain 2 files:**

 1. The plugin itself very light = pxw-test.php  
 2. The file to download = file.txt

Which I Uploaded Today, the link will be valid for 30 days only and I will Update the link when expired, if it does not work please let me know..

Before download You can have a look at the pxw-test.php file Content (The Plugin Itself) Below:

    <?php
        	/*
        		Plugin Name: Test Headers
        		Description: Download File Test Plugin for Headers Response errors.
        		Version:     1.0
        		Author:      Tester
        		Text Domain: pxw_test
        	*/
        	//Exit if accessed directly
        	if ( ! defined( 'ABSPATH' ) ){
        		exit;
        	}
        	//Admin Menu
        	function pxw_test_menu() {
        		//create custom top-level menu
        		add_menu_page( 
        		'Dashboard',                                  	         	
        		'Test Headers',                              	             	
        		'manage_options',                   	                 
        		'pxw_test',      		                                
        		'pxw_test_dashboard_page',
        		null,
        		20                     		                               
        		);
        	}
        	//Function needed to display top-level page as the main page 
        	function pxw_test_dashboard_page(){
        		//Download 
        		if(isset($_POST['download'])){
        		    //File To Download
        			$filename = 'file.txt';
        			// http headers for Downloads
        			header("Pragma: public");
        			header("Expires: 0");
        			header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
        			header("Content-Type: application/force-download");
        			header("Content-Type: application/octet-stream");
        			header("Content-Type: application/download");
        			header("Content-Disposition: attachment; filename=$filename");
        			header("Content-Transfer-Encoding: binary");
        			header("Content-Length: ".filesize(plugin_dir_path(__FILE__).$filename));
        			//You can try the following 
        			// while (ob_get_level()) {
        			// ob_end_clean();
        			// }
        			// @readfile(plugin_dir_path(__FILE__).$filename);
        		}
        	else{}
        ?>
        	<form action="" method="POST" enctype="multipart/form-data" style="text-align:center;padding: 100px 0;">
        		<button type="submit" onclick="this.form.submit();" style="background:red;padding:10px;color:#fff;cursor:pointer;"><b>Download File</b></button>
        		<input type="hidden" name="download" />
        	</form>
        <?php
        	}
        	//Finally Display the menu 
        	add_action('admin_menu', 'pxw_test_menu' );?>

Before Usual Nonsense Reply such as 'check the spaces before/after <?php / ?> tags' and similar attempt solutions, 

**Please check the test plugin** so you can spot easily the real problem or solution for my case.

<!-- What do you expect the result to be? -->

> My goal is to **Download** a file and **delete** the file after Download.

**Of course I am not looking for:** 

**function.php** solution as I am building a plugin. 

**But Anything short and manageable from my plugin folder is welcomed.**
Such as: 


 1. PHP

 2. javascript

 3. Jquery

 4. Html5 such as `<iframe>` **but not** the `<a download></a>` as I will need to make actions such as delete file after download and if file  downloaded or not.  

I hope someone of the wordpress guru out there can unveil the mistery behind it and explain exactly in short why that happens and how to resolve it.

I thank you all very much in advance for your help and interest!!
 
<!-- What is the actual result you get? (Please include any errors.) -->


 


  [1]: https://ufile.io/ld2v4