PHP download Excel file,
1, in the process of downloading do not output any non-file information, such as Echo log information. Otherwise, the downloaded file cannot be opened, prompting for incorrect formatting or corrupted file.
2, the output of the Excel format must be and the suffix name to save always, no will also prompt format error or file is corrupted
Copy the Code code as follows:
if (File_exists (Cache_path. $file _name)) {
$this->logger->error (' File Realpath: '. Realpath (Cache_path. $file _name));
Header (' Pragma:public ');
Header (' expires:0 ');
Header (' Content-encoding:none ');
Header (' Cache-control:must-revalidate, post-check=0, pre-check=0 ');
Header (' Cache-control:public ');
Header (' Content-type:application/vnd.ms-excel ');
Header (' Content-description:file Transfer ');
Header (' content-disposition:attachment; filename= '. $file _name);
Header (' content-transfer-encoding:binary ');
Header (' Content-length: ' FileSize (cache_path. $file _name));
ReadFile (Cache_path. $file _name);
} else {
$this->logger->error (' Export model: ' $id. ' ERROR: No production files ');
Echo ';
}