0

I have problem with writing csv file using fputcsv. Its putting the page html also into the csv file. Whats wrong with my code ?

        //Excel header
        header("Content-Disposition: attachment; filename=\"Delivery_Reports.csv\";" );
        header("Content-type: application/vnd.ms-excel");

        $out = fopen("php://output", 'w');
        $flag = false;
        // $result = mysql_query("SELECT * FROM senderids ") or die('Query failed!'); 
        //$sel="SELECT number as MobileNumber ,snum as Sender , msg as Subject ,crdate as Date ,status FROM  savemsg WHERE userID='".$_SESSION['id']."' ".$str." ORDER BY sn DESC ";
        $result = mysql_query("SELECT `count`, `dnd`, `credit`, `sender_id`, `to`, `message`, `status` FROM `reports` WHERE `unq_id` = '$dlr_id'");
        while(false !== ($row = mysql_fetch_assoc($result))){
            if(!$flag){
                $list = array( "Total"=>"Total","DND"=>"DND","Credits"=>"Credits","From"=>"From","To"=>"To","Message"=>"Message","Status"=>"Status", );
                // display field/column names as first row 
                fputcsv($out, array_keys($list), ',', '"');
                $flag = true; 
            }
            // array_walk($row, 'cleanData');
            fputcsv($out, array_values($row), ',', '"');
        }
        fclose($out);
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by ddsuresh
1

You need to give exit; after fclose($out); to avoid further printing of html tags.

Votes + Comments
Its worked ! Thank you very much
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.