Home > Backend Development > PHP Tutorial > Solution to the problem that Excel cannot be opened when downloaded by PHP_PHP Tutorial

Solution to the problem that Excel cannot be opened when downloaded by PHP_PHP Tutorial

WBOY
Release: 2016-07-13 17:18:30
Original
917 people have browsed it

php download excel file,
1. Do not output any non-file information during the download process, such as echo log information. Otherwise, the downloaded file cannot be opened, prompting a format error or the file is damaged.
2. The output excel format must be saved with the suffix name, otherwise it will prompt a format error or the file will be damaged

Copy code The code is 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: File not produced');
echo ' <script>alert('export error, file not exists!')</script>';
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621669.htmlTechArticlephp download excel file, 1. Do not output any non-file information during the download process, such as echo log information. Otherwise, the downloaded file cannot be opened, prompting a format error or the file is damaged...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template