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!