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);

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

commented: Its worked ! Thank you very much +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.