注意:如果输出的是中文信息的话,要注意字符编码的格式转换!

但如果我想下载的是服务器中保存的xls文件呢?

经过查看php手册:发现很简单就可以实现这个功能了,使用到了一个 readfile">
Home > Backend Development > PHP Tutorial > Use PHP to download xls files (written by myself)_PHP tutorial

Use PHP to download xls files (written by myself)_PHP tutorial

WBOY
Release: 2016-07-13 10:32:10
Original
1181 people have browsed it

When I was looking at the ECSHOP source code yesterday, I came across something I had never learned before - how to use php to download xls files. Based on its source code, I implemented it and successfully achieved this effect.

Source code:

Copy code The code is as follows:

/*
*@Description: Download xls table
*
*
*/
function downloadXls($filename=''){
$filename = !empty($filename) ? $filename : die('nothing');

//The function of header is to create a new downloaded test.xls
header("Content-Type: application/vnd.ms-excel; charset=utf8" );
header("Content-Disposition: attachment; filename=$filename");

//The content that needs to be output here is directly output to the test.xls file
echo 'This is the test!';
exit;
}

$fileName = 'test.xls';
downloadXls($fileName);
?>

Effect:

Use PHP to download xls files (written by myself)_PHP tutorial

Note: If the output is Chinese information, pay attention to the format conversion of character encoding!

But what if I want to download an xls file saved in the server?

After checking the php manual: I found that this function can be implemented very easily, using a readfile function. The code is as follows:
Copy code The code is as follows:

/*
*@Description :Download xls table
*
*
*/
function downloadXls($filename=''){
$filename = !empty($filename) ? $filename : die('nothing ');

//The function of header is to create a new downloaded test.xls
header("Content-Type: application/vnd.ms-excel; charset=utf8");
header("Content-Disposition: attachment; filename=$filename");

//Here is the file that needs to be output
readfile($filename);
}

$fileName = 'test.xls';
downloadXls($fileName);
?>

Effect:

Use PHP to download xls files (written by myself)_PHP tutorial

Let’s expand a little further: What if I want to download a txt file, what about a pdf file?

The way to achieve this is to modify the content in Content-Type in the header output!

If there is anything missing, please point it out!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/756338.htmlTechArticleWhen I was looking at the ECSHOP source code yesterday, I came across something I had never learned before - how to use php to download xls document. According to its source code, I implemented it and successfully achieved this...
Related labels:
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