Summary of PHP output non-HTML format files

Source: Internet
Author: User
    1. $file = ' a.pdf ';
    2. if (file_exists ($file)) {
    3. Header (' Content-description:file Transfer ');
    4. Header (' Content-type:application/octet-stream ');
    5. Header (' content-disposition:attachment; Filename= '. basename ($file));
    6. Header (' content-transfer-encoding:binary ');
    7. Header (' expires:0 ');
    8. Header (' Cache-control:must-revalidate, post-check=0, pre-check=0 ');
    9. Header (' Pragma:public ');
    10. B_clean ();
    11. Flush ();
    12. ReadFile ($file);
    13. Exit
    14. }
    15. ?>
Copy Code

2. Output generated files (such as CSV PDF, etc.) sometimes the system that will output the generated files, mainly generate csv,pdf, or packaging multiple files for the zip format to download, for this part, some of the implementation method is to generate the output as a file and then through the file download, Finally delete the makefile, you can actually output the file by Php://output directly, the following CSV output as an example.

    1. Header (' Content-description:file Transfer ');
    2. Header (' Content-type:application/octet-stream ');
    3. Header (' content-disposition:attachment; Filename=a.csv ');
    4. Header (' content-transfer-encoding:binary ');
    5. Header (' expires:0 ');
    6. Header (' Cache-control:must-revalidate, post-check=0, pre-check=0 ');
    7. Header (' Pragma:public ');
    8. Ob_clean ();
    9. Flush ();
    10. $rowarr =array (Array (' 1 ', ' 2 ', ' 3 '), Array (' 1 ', ' 2 ', ' 3 '));
    11. $fp =fopen (' Php://output ', ' W ');
    12. foreach ($rowarr as $row) {
    13. Fputcsv ($fp, $row);
    14. }
    15. Fclose ($FP);
    16. Exit
    17. ?>
Copy Code

3. Get the contents of the generated file, do the output after processing to get the contents of the generated file is generally Mr. Cheng file, then read, and finally delete, in fact, this can use Php://temp to do the operation, the following is still a CSV example

    1. Header (' Content-description:file Transfer ');
    2. Header (' Content-type:application/octet-stream ');
    3. Header (' content-disposition:attachment; Filename=a.csv ');
    4. Header (' content-transfer-encoding:binary ');
    5. Header (' expires:0 ');
    6. Header (' Cache-control:must-revalidate, post-check=0, pre-check=0 ');
    7. Header (' Pragma:public ');
    8. Ob_clean ();
    9. Flush ();
    10. $rowarr =array (Array (' 1 ', ' 2 ', ' Chinese '), Array (' 1 ', ' 2 ', ' 3 '));
    11. $fp =fopen (' php://temp ', ' r+ ');
    12. foreach ($rowarr as $row) {
    13. Fputcsv ($fp, $row);
    14. }
    15. Rewind ($FP);
    16. $filecontent =stream_get_contents ($FP);
    17. Fclose ($FP);
    18. Working with $filecontent content
    19. $filecontent =iconv (' UTF-8 ', ' GBK ', $filecontent);
    20. Echo $filecontent; Output
    21. Exit
    22. ?>
Copy Code

PHP in the Input/output streams function is very powerful, with good, can simplify coding, improve efficiency, we recommend you specifically into the OH.

  • 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.