Download xls files with PHP (self-written) _php tutorial

Source: Internet
Author: User
Tags readfile
Yesterday look at Ecshop source, met a little did not learn just-how to use PHP implementation to download XLS file. According to its source code, I have implemented a bit, successfully achieved this effect.

Source:
Copy CodeThe code is as follows:
/*
* @Description: Download xls table
*
*
*/
function Downloadxls ($filename = ") {
$filename =!empty ($filename)? $filename: Die (' nothing ');

The function of the header is to create a new downloaded Test.xls
Header ("Content-type:application/vnd.ms-excel; Charset=utf8 ");
Header ("content-disposition:attachment; Filename= $filename ");

This needs to be output output directly to the Test.xls file.
Echo ' This is the test! ';
Exit
}

$fileName = ' Test.xls ';
Downloadxls ($fileName);
?>

Effect:



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

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

After reviewing the PHP manual: find it easy to implement this function, using a ReadFile function. The code is as follows:
Copy CodeThe code is as follows:
/*
* @Description: Download xls table
*
*
*/
function Downloadxls ($filename = ") {
$filename =!empty ($filename)? $filename: Die (' nothing ');

The function of the 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 exported
ReadFile ($filename);
}

$fileName = ' Test.xls ';
Downloadxls ($fileName);
?>

Effect:



Another extension: What if I want to download a txt file, a PDF file?

The way to do this is to modify the contents of the Content-type in the header output!

There is not much place, please the Great God pointed out!

http://www.bkjia.com/PHPjc/756338.html www.bkjia.com true http://www.bkjia.com/PHPjc/756338.html techarticle yesterday look at Ecshop source, met a little did not learn just-how to use PHP implementation to download XLS file. According to its source code, I realized a bit, successfully realized this ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.