Home > Backend Development > PHP Problem > What should I do if Excel cannot be opened after downloading it from PHP?

What should I do if Excel cannot be opened after downloading it from PHP?

藏色散人
Release: 2023-03-09 13:38:02
Original
2369 people have browsed it

Solutions to the problem that PHP cannot open Excel after downloading: 1. Do not output any non-file information during the download process; 2. The output Excel format must be consistent with the suffix name saved; 3. Check whether the file is downloaded. Just be complete.

What should I do if Excel cannot be opened after downloading it from PHP?

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

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, indicating 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 is damaged

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.' 错误:未生产文件');
echo &#39;<script>alert(\&#39;export error, file not
exists!\&#39;)</script>&#39;;
}
Copy after login

3. File error , please check whether the file is downloaded completely.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What should I do if Excel cannot be opened after downloading it from PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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