Question about exporting csv from php

Source: Internet
Author: User
Question about exporting csv from php Online, etc. ask if you know why html source code appears in the file when php downloads a csv fileThe xls file is normal.


Reply to discussion (solution)

Based on your bad description, I can only give one bad answer ?? Your code is faulty.

I put the csv file generated by php on the page, click to download, open the download csv file, and the content finally shows a piece of html code. do you understand!

Generated
To see your code

If ($ opt = '') {if (! In_array ($ userid, $ okuser) {echo "you are not authorized to browse this page"; exit;} else {$ web_folder = "/tj /"; $ log_folder = "/tj/";} $ logFile = array (); if (is_dir ($ log_folder) {if ($ dh = opendir ($ log_folder )) {while ($ file = readdir ($ dh ))! = False) {if ($ file! = "." & $ File! = ".. ") {$ LogFile [$ file] = $ file. "& ver = ". mt_rand () ;}}} arsort ($ logFile); $ smarty-> assign ("logfile", $ logFile); $ smarty-> assign ("web_folder ", $ web_folder);} elseif ($ opt = 'download') {if (! In_array ($ userid, $ okuser) {echo "you are not authorized to browse this page"; exit;} $ filename = $ cgi-> filename; header ("Content-Type: text/csv "); header (" Content-Type: text/xls "); header (" Content-Disposition: attachment; filename = ". $ filename); readfile ("/tj /". $ filename);} else {echo "you are not authorized to browse this page"; exit ;}
This is the php code
{If $ opt = ''} {include file =" menus.htm "}

Statistical table:




Download link
{Foreach from = $ logfile key = k item = v}
  • {$ K}
  • {/Foreach}

    The a.csv file generated by PHP is correct, but the html source code of page a is also output to the csv file by downloading page.
    Depressing!

    The last table is not closed.

    The problem of pasting is closed. it seems that this problem is rare!

    Header ("Content-Type: text/csv ");
    Header ("Content-Type: text/xls ");

    How to send two content-type headers

    Yes, because the files I want to download are in xls or csv format.
    I checked and found the problem.



    readfile("/tj/".$filename);
    {Include file = "foot.htm"} {/if} {if $ opt = 'download'} {/if}This is the html code This sentence,Read the file to a die (); suppress the page output; otherwise, the exported excel file will display the html source code of the requested page;Solution: Add a die () or exit; after redfilie!

    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.