2012-10-03 16 views
1

我想要做的是,調用一個類將mysql數據導出到另一個類的excel中。下面是我使用的代碼(我省略了打開連接的代碼):無法修改靜態功能上的頭信息

//This is the class for calling the `ExportToExcel` class 
Class SampleClass { 
    public static function mainFunction() { 
     ExportToExcel::exportToExcel(); 
    } 
} 

Class ExportToExcel { 
    public static function exportToExcel() { 
     $DB = DB::Open(); //Open the database 

     $csv_terminated = "\n"; 
     $csv_separator = ","; 
     $csv_enclosed = '"'; 
     $csv_escaped = "\\"; 

     $sql_query = "The Query goes here..."; 

     $result = $DB->qry($sql_query, 2, TRUE); //Run the query 
     $fields_cnt = 0; 
     $schema_insert = ''; 

     while ($finfo = mysqli_fetch_field($result)) { 
      $fields_cnt++; 
      $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes($finfo->name)) . $csv_enclosed; 
      $schema_insert .= $l; 
      $schema_insert .= $csv_separator; 
     } 

     $out = trim(substr($schema_insert, 0, -1)); 
     $out .= $csv_terminated; 

     while ($row = mysqli_fetch_array($result)) { 
      $schema_insert = ''; 
      for ($j = 0; $j < $fields_cnt; $j++) { 
       if ($row[$j] == '0' || $row[$j] != '') { 
        if ($csv_enclosed == '') $schema_insert .= $row[$j]; 
        else { 
         $schema_insert .= $csv_enclosed . 
         str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed; 
        } 
       } else $schema_insert .= ''; 

       if ($j < $fields_cnt - 1) $schema_insert .= $csv_separator; 
      } 

      $out .= $schema_insert; 
      $out .= $csv_terminated; 
     } 

     header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); //--> WARNING 
     header("Content-Length: " . strlen($out)); //--> WARNING 
     header("Content-type: text/x-csv"); //--> WARNING 
     header("Content-Disposition: attachment; filename=somefile.csv"); //--> WARNING 
     echo $out; 
     exit; 
    } 
} 

但它會返回錯誤:

Warning: Cannot modify header information - headers already sent by (output started at 'files' on line XX 

當我直接運行的代碼,而無需任何階級和功能,它的工作原理。我試圖檢查使用var_dump(headers_list())標題列表,我有:

array(5) { 
    [0]=> string(23) "X-Powered-By: PHP/5.4.4" 
    [1]=> string(38) "Expires: Thu, 19 Nov 1981 08:52:00 GMT" 
    [2]=> string(77) "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0" 
    [3]=> string(16) "Pragma: no-cache" 
    [4]=> string(23) "Content-type: text/html" 
} 

我試圖刪除使用header_remove();頭,但它是一樣的。如何解決問題?謝謝...

+0

'頭已經發送...'誰? –

回答

1

嘗試在結束

+0

絕對! = D,我試着將代碼放在頁面上的最後一頁。有用!謝謝:) –

+1

最後使用'ob_end_flush()'。 – StaticVariable

+0

_start_在哪裏,_end_在哪裏? –

2

這增加ob_start()開始和ob_flush()意味着,在某些時候,你要發送的文本瀏覽器,你做你的各種頭()調用之前。

檢查你的代碼,看看它是否有打印響應它正在做什麼。這可以是來自代碼的錯誤或某種打印的任何內容。

不幸的是,如果沒有完整的代碼,我們將無法確定在header()調用之前正在向瀏覽器發送文本的代碼的特定部分。

編輯:

同時檢查是否存在空格。如果在調用header()之前將<?php ?>之外的空格傳遞給瀏覽器,那麼也可以這樣做。

+0

嗨,感謝您的建議:) –

1

這個問題,當我們發送一些輸出到瀏覽器,比header.You應該找出一個空格或任何header()header("location")echo發生。

參考這篇文章Header already send problem

+0

嗨,感謝您的代碼..但我正在使用'ob_start()'和'ob_end_flush()',因爲你說,它的工作:) –