Questions about PHP exporting CSV

Source: Internet
Author: User
online, etc., asked if you know, PHP when downloading CSV file in the file why HTML sourceXLS file is normal


Reply to discussion (solution)

Based on your bad description, I can only give a bad answer?? There's something wrong with your code.

I put PHP generated CSV file on the page, click to download, open the download CSV Open after the content of the last HTML code to understand it!

When you build it, you have it.
To see your code

if ($opt = = ") {if (!in_array ($userid, $okuser)) {echo" You do not have permission 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", $w Eb_folder);}            ElseIf ($opt = = ' Download ') {if (!in_array ($userid, $okuser)) {echo "You do not have permission 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 do not have permission to browse this page"; Exit;}
This is the PHP code
{if $opt = = '} {include file= "menus.htm"} Statistics:


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

    PHP generated a.csv file can certainly be right, but through a page download, a page of the HTML source also output to the CSV file
    More depressed!

    Visually your last table is not closed.

    I pasted the problem closed, it seems that the problem is seldom met Ah!

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

    How to send two Content-type head

    Yes, because I'm going to download something with the XLS format also has CSV format
    I checked, found the problem, the problem is



  • readfile ("/tj/". $filename); 
    {include file= "foot.htm"}{/if}{if $opt = = ' Download '}{/if} This is HTML code This sentence, read the document to a die (); Suppress page output, otherwise, the exported Excel will appear the HTML source of the request page; Workaround: Add a die () or exit after the redfilie sentence.

    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.