使用PHP从服务器下载Excel

Can anyone help me to write code on PHP to download Excel File from the server.

I have created below codes using header and readfile but the downloaded file was corrupted.

//content type
header('Content-type: application/vnd.ms-excel');
//open/save dialog box
header('Content-Disposition: attachment; filename='.$fileName);
//read from server and write to buffer
readfile($reportPath);

Can anyone help me on the best way to download existing Excel file from the server.

Please see below image of data after downloadedenter image description here

I suggest to use the X-SENDFILE header. https://tn123.org/mod_xsendfile/

I use something like the following:

header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Length: ".filesize($path));
readfile($path);
exit;

Make sure you're not outputting anything before or after the readfile.

ob_clean();

put that code before all header declaration