[PHP] 資料匯出CSV Excel header()問題

摘要:[PHP] 資料匯出CSV Excel header()問題

前幾天在寫另一套系統的CSV檔匯出時,遇到一個以前遇到的問題;由於系統是新裝的,因此PHP、MySQL、Apache等的設定都還是預設,所以有些功能還沒辦法正常使用。

當我要進行匯出測試時,出現下列錯誤訊息:

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\mypage\mypage.php:21) in C:\AppServ\www\mypage\system\outputCSVorderform.php on line 15

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\mypage\include\config.php:21) in C:\AppServ\www\mypage\system\outputCSVorderform.php on line 16

照字面上的翻譯就是我的header重覆呼叫了。既然我要做匯出,我就必須要再宣告一次header(),但是又不一定真的寫在頁首(或許可以,但我不會)。於是上網查了一下資料(其實很簡單,把錯誤資訊「Cannot modify header information - headers already sent by 」貼到google按Enter就一堆答案,而這篇文章也將會成為茫茫答海之一 :p )

解法很簡單,進入php.ini後搜尋一個output_buffering的參數設定,將「Off」改成「On」,簡單來說,這個設定你可以選擇開啟或是直接限制他的大小,修改完後restart Apache就可以了。

*Note:我是在Windows XP SP3下進行測試,所以在php.ini是在C:\WINDOWS\php.ini,至於On的「O」是大寫小寫,應該是視系統而定,在WINDOWS下是不分大小寫(測試OK!),但是在Linux下小弟就不確定了(Linux大小寫視為不同),因小弟手邊目前沒有Linux的系統還請有經驗的前輩分享一下^^"。

Keyword:  "header() problem of export files to CSV, EXCEL", "export file CSV header", "headers already send by"

相關網頁: [PHP] 資料匯出CSV Excel